mp2.js 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. JSMpeg.Decoder.MP2Audio = (function(){ "use strict";
  2. // Based on kjmp2 by Martin J. Fiedler
  3. // http://keyj.emphy.de/kjmp2/
  4. var MP2 = function(options) {
  5. JSMpeg.Decoder.Base.call(this, options);
  6. this.onDecodeCallback = options.onAudioDecode;
  7. var bufferSize = options.audioBufferSize || 128*1024;
  8. var bufferMode = options.streaming
  9. ? JSMpeg.BitBuffer.MODE.EVICT
  10. : JSMpeg.BitBuffer.MODE.EXPAND;
  11. this.bits = new JSMpeg.BitBuffer(bufferSize, bufferMode);
  12. this.left = new Float32Array(1152);
  13. this.right = new Float32Array(1152);
  14. this.sampleRate = 44100;
  15. this.D = new Float32Array(1024);
  16. this.D.set(MP2.SYNTHESIS_WINDOW, 0);
  17. this.D.set(MP2.SYNTHESIS_WINDOW, 512);
  18. this.V = new Float32Array(1024);
  19. this.U = new Int32Array(32);
  20. this.VPos = 0;
  21. this.allocation = [new Array(32), new Array(32)];
  22. this.scaleFactorInfo = [new Uint8Array(32), new Uint8Array(32)];
  23. this.scaleFactor = [new Array(32), new Array(32)];
  24. this.sample = [new Array(32), new Array(32)];
  25. for (var j = 0; j < 2; j++) {
  26. for (var i = 0; i < 32; i++) {
  27. this.scaleFactor[j][i] = [0, 0, 0];
  28. this.sample[j][i] = [0, 0, 0];
  29. }
  30. }
  31. };
  32. MP2.prototype = Object.create(JSMpeg.Decoder.Base.prototype);
  33. MP2.prototype.constructor = MP2;
  34. MP2.prototype.decode = function() {
  35. var startTime = JSMpeg.Now();
  36. var pos = this.bits.index >> 3;
  37. if (pos >= this.bits.byteLength) {
  38. return false;
  39. }
  40. var decoded = this.decodeFrame(this.left, this.right);
  41. this.bits.index = (pos + decoded) << 3;
  42. if (!decoded) {
  43. return false;
  44. }
  45. if (this.destination) {
  46. this.destination.play(this.sampleRate, this.left, this.right);
  47. }
  48. this.advanceDecodedTime(this.left.length / this.sampleRate);
  49. var elapsedTime = JSMpeg.Now() - startTime;
  50. if (this.onDecodeCallback) {
  51. this.onDecodeCallback(this, elapsedTime);
  52. }
  53. return true;
  54. };
  55. MP2.prototype.getCurrentTime = function() {
  56. var enqueuedTime = this.destination ? this.destination.enqueuedTime : 0;
  57. return this.decodedTime - enqueuedTime;
  58. };
  59. MP2.prototype.decodeFrame = function(left, right) {
  60. // Check for valid header: syncword OK, MPEG-Audio Layer 2
  61. var sync = this.bits.read(11),
  62. version = this.bits.read(2),
  63. layer = this.bits.read(2),
  64. hasCRC = !this.bits.read(1);
  65. if (
  66. sync !== MP2.FRAME_SYNC ||
  67. version !== MP2.VERSION.MPEG_1 ||
  68. layer !== MP2.LAYER.II
  69. ) {
  70. return 0; // Invalid header or unsupported version
  71. }
  72. var bitrateIndex = this.bits.read(4) - 1;
  73. if (bitrateIndex > 13) {
  74. return 0; // Invalid bit rate or 'free format'
  75. }
  76. var sampleRateIndex = this.bits.read(2);
  77. var sampleRate = MP2.SAMPLE_RATE[sampleRateIndex];
  78. if (sampleRateIndex === 3) {
  79. return 0; // Invalid sample rate
  80. }
  81. if (version === MP2.VERSION.MPEG_2) {
  82. sampleRateIndex += 4;
  83. bitrateIndex += 14;
  84. }
  85. var padding = this.bits.read(1),
  86. privat = this.bits.read(1),
  87. mode = this.bits.read(2);
  88. // Parse the mode_extension, set up the stereo bound
  89. var bound = 0;
  90. if (mode === MP2.MODE.JOINT_STEREO) {
  91. bound = (this.bits.read(2) + 1) << 2;
  92. }
  93. else {
  94. this.bits.skip(2);
  95. bound = (mode === MP2.MODE.MONO) ? 0 : 32;
  96. }
  97. // Discard the last 4 bits of the header and the CRC value, if present
  98. this.bits.skip(4);
  99. if (hasCRC) {
  100. this.bits.skip(16);
  101. }
  102. // Compute the frame size
  103. var bitrate = MP2.BIT_RATE[bitrateIndex],
  104. sampleRate = MP2.SAMPLE_RATE[sampleRateIndex],
  105. frameSize = ((144000 * bitrate / sampleRate) + padding)|0;
  106. // Prepare the quantizer table lookups
  107. var tab3 = 0;
  108. var sblimit = 0;
  109. if (version === MP2.VERSION.MPEG_2) {
  110. // MPEG-2 (LSR)
  111. tab3 = 2;
  112. sblimit = 30;
  113. }
  114. else {
  115. // MPEG-1
  116. var tab1 = (mode === MP2.MODE.MONO) ? 0 : 1;
  117. var tab2 = MP2.QUANT_LUT_STEP_1[tab1][bitrateIndex];
  118. tab3 = MP2.QUANT_LUT_STEP_2[tab2][sampleRateIndex];
  119. sblimit = tab3 & 63;
  120. tab3 >>= 6;
  121. }
  122. if (bound > sblimit) {
  123. bound = sblimit;
  124. }
  125. // Read the allocation information
  126. for (var sb = 0; sb < bound; sb++) {
  127. this.allocation[0][sb] = this.readAllocation(sb, tab3);
  128. this.allocation[1][sb] = this.readAllocation(sb, tab3);
  129. }
  130. for (var sb = bound; sb < sblimit; sb++) {
  131. this.allocation[0][sb] =
  132. this.allocation[1][sb] =
  133. this.readAllocation(sb, tab3);
  134. }
  135. // Read scale factor selector information
  136. var channels = (mode === MP2.MODE.MONO) ? 1 : 2;
  137. for (var sb = 0; sb < sblimit; sb++) {
  138. for (ch = 0; ch < channels; ch++) {
  139. if (this.allocation[ch][sb]) {
  140. this.scaleFactorInfo[ch][sb] = this.bits.read(2);
  141. }
  142. }
  143. if (mode === MP2.MODE.MONO) {
  144. this.scaleFactorInfo[1][sb] = this.scaleFactorInfo[0][sb];
  145. }
  146. }
  147. // Read scale factors
  148. for (var sb = 0; sb < sblimit; sb++) {
  149. for (var ch = 0; ch < channels; ch++) {
  150. if (this.allocation[ch][sb]) {
  151. var sf = this.scaleFactor[ch][sb];
  152. switch (this.scaleFactorInfo[ch][sb]) {
  153. case 0:
  154. sf[0] = this.bits.read(6);
  155. sf[1] = this.bits.read(6);
  156. sf[2] = this.bits.read(6);
  157. break;
  158. case 1:
  159. sf[0] =
  160. sf[1] = this.bits.read(6);
  161. sf[2] = this.bits.read(6);
  162. break;
  163. case 2:
  164. sf[0] =
  165. sf[1] =
  166. sf[2] = this.bits.read(6);
  167. break;
  168. case 3:
  169. sf[0] = this.bits.read(6);
  170. sf[1] =
  171. sf[2] = this.bits.read(6);
  172. break;
  173. }
  174. }
  175. }
  176. if (mode === MP2.MODE.MONO) {
  177. this.scaleFactor[1][sb][0] = this.scaleFactor[0][sb][0];
  178. this.scaleFactor[1][sb][1] = this.scaleFactor[0][sb][1];
  179. this.scaleFactor[1][sb][2] = this.scaleFactor[0][sb][2];
  180. }
  181. }
  182. // Coefficient input and reconstruction
  183. var outPos = 0;
  184. for (var part = 0; part < 3; part++) {
  185. for (var granule = 0; granule < 4; granule++) {
  186. // Read the samples
  187. for (var sb = 0; sb < bound; sb++) {
  188. this.readSamples(0, sb, part);
  189. this.readSamples(1, sb, part);
  190. }
  191. for (var sb = bound; sb < sblimit; sb++) {
  192. this.readSamples(0, sb, part);
  193. this.sample[1][sb][0] = this.sample[0][sb][0];
  194. this.sample[1][sb][1] = this.sample[0][sb][1];
  195. this.sample[1][sb][2] = this.sample[0][sb][2];
  196. }
  197. for (var sb = sblimit; sb < 32; sb++) {
  198. this.sample[0][sb][0] = 0;
  199. this.sample[0][sb][1] = 0;
  200. this.sample[0][sb][2] = 0;
  201. this.sample[1][sb][0] = 0;
  202. this.sample[1][sb][1] = 0;
  203. this.sample[1][sb][2] = 0;
  204. }
  205. // Synthesis loop
  206. for (var p = 0; p < 3; p++) {
  207. // Shifting step
  208. this.VPos = (this.VPos - 64) & 1023;
  209. for (var ch = 0; ch < 2; ch++) {
  210. MP2.MatrixTransform(this.sample[ch], p, this.V, this.VPos);
  211. // Build U, windowing, calculate output
  212. JSMpeg.Fill(this.U, 0);
  213. var dIndex = 512 - (this.VPos >> 1);
  214. var vIndex = (this.VPos % 128) >> 1;
  215. while (vIndex < 1024) {
  216. for (var i = 0; i < 32; ++i) {
  217. this.U[i] += this.D[dIndex++] * this.V[vIndex++];
  218. }
  219. vIndex += 128-32;
  220. dIndex += 64-32;
  221. }
  222. vIndex = (128-32 + 1024) - vIndex;
  223. dIndex -= (512 - 32);
  224. while (vIndex < 1024) {
  225. for (var i = 0; i < 32; ++i) {
  226. this.U[i] += this.D[dIndex++] * this.V[vIndex++];
  227. }
  228. vIndex += 128-32;
  229. dIndex += 64-32;
  230. }
  231. // Output samples
  232. var outChannel = ch === 0 ? left : right;
  233. for (var j = 0; j < 32; j++) {
  234. outChannel[outPos + j] = this.U[j] / 2147418112;
  235. }
  236. } // End of synthesis channel loop
  237. outPos += 32;
  238. } // End of synthesis sub-block loop
  239. } // Decoding of the granule finished
  240. }
  241. this.sampleRate = sampleRate;
  242. return frameSize;
  243. };
  244. MP2.prototype.readAllocation = function(sb, tab3) {
  245. var tab4 = MP2.QUANT_LUT_STEP_3[tab3][sb];
  246. var qtab = MP2.QUANT_LUT_STEP4[tab4 & 15][this.bits.read(tab4 >> 4)];
  247. return qtab ? (MP2.QUANT_TAB[qtab - 1]) : 0;
  248. };
  249. MP2.prototype.readSamples = function(ch, sb, part) {
  250. var q = this.allocation[ch][sb],
  251. sf = this.scaleFactor[ch][sb][part],
  252. sample = this.sample[ch][sb],
  253. val = 0;
  254. if (!q) {
  255. // No bits allocated for this subband
  256. sample[0] = sample[1] = sample[2] = 0;
  257. return;
  258. }
  259. // Resolve scalefactor
  260. if (sf === 63) {
  261. sf = 0;
  262. }
  263. else {
  264. var shift = (sf / 3)|0;
  265. sf = (MP2.SCALEFACTOR_BASE[sf % 3] + ((1 << shift) >> 1)) >> shift;
  266. }
  267. // Decode samples
  268. var adj = q.levels;
  269. if (q.group) {
  270. // Decode grouped samples
  271. val = this.bits.read(q.bits);
  272. sample[0] = val % adj;
  273. val = (val / adj)|0;
  274. sample[1] = val % adj;
  275. sample[2] = (val / adj)|0;
  276. }
  277. else {
  278. // Decode direct samples
  279. sample[0] = this.bits.read(q.bits);
  280. sample[1] = this.bits.read(q.bits);
  281. sample[2] = this.bits.read(q.bits);
  282. }
  283. // Postmultiply samples
  284. var scale = (65536 / (adj + 1))|0;
  285. adj = ((adj + 1) >> 1) - 1;
  286. val = (adj - sample[0]) * scale;
  287. sample[0] = (val * (sf >> 12) + ((val * (sf & 4095) + 2048) >> 12)) >> 12;
  288. val = (adj - sample[1]) * scale;
  289. sample[1] = (val * (sf >> 12) + ((val * (sf & 4095) + 2048) >> 12)) >> 12;
  290. val = (adj - sample[2]) * scale;
  291. sample[2] = (val * (sf >> 12) + ((val * (sf & 4095) + 2048) >> 12)) >> 12;
  292. };
  293. MP2.MatrixTransform = function(s, ss, d, dp) {
  294. var t01, t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t12,
  295. t13, t14, t15, t16, t17, t18, t19, t20, t21, t22, t23, t24,
  296. t25, t26, t27, t28, t29, t30, t31, t32, t33;
  297. t01 = s[ 0][ss] + s[31][ss]; t02 = (s[ 0][ss] - s[31][ss]) * 0.500602998235;
  298. t03 = s[ 1][ss] + s[30][ss]; t04 = (s[ 1][ss] - s[30][ss]) * 0.505470959898;
  299. t05 = s[ 2][ss] + s[29][ss]; t06 = (s[ 2][ss] - s[29][ss]) * 0.515447309923;
  300. t07 = s[ 3][ss] + s[28][ss]; t08 = (s[ 3][ss] - s[28][ss]) * 0.53104259109;
  301. t09 = s[ 4][ss] + s[27][ss]; t10 = (s[ 4][ss] - s[27][ss]) * 0.553103896034;
  302. t11 = s[ 5][ss] + s[26][ss]; t12 = (s[ 5][ss] - s[26][ss]) * 0.582934968206;
  303. t13 = s[ 6][ss] + s[25][ss]; t14 = (s[ 6][ss] - s[25][ss]) * 0.622504123036;
  304. t15 = s[ 7][ss] + s[24][ss]; t16 = (s[ 7][ss] - s[24][ss]) * 0.674808341455;
  305. t17 = s[ 8][ss] + s[23][ss]; t18 = (s[ 8][ss] - s[23][ss]) * 0.744536271002;
  306. t19 = s[ 9][ss] + s[22][ss]; t20 = (s[ 9][ss] - s[22][ss]) * 0.839349645416;
  307. t21 = s[10][ss] + s[21][ss]; t22 = (s[10][ss] - s[21][ss]) * 0.972568237862;
  308. t23 = s[11][ss] + s[20][ss]; t24 = (s[11][ss] - s[20][ss]) * 1.16943993343;
  309. t25 = s[12][ss] + s[19][ss]; t26 = (s[12][ss] - s[19][ss]) * 1.48416461631;
  310. t27 = s[13][ss] + s[18][ss]; t28 = (s[13][ss] - s[18][ss]) * 2.05778100995;
  311. t29 = s[14][ss] + s[17][ss]; t30 = (s[14][ss] - s[17][ss]) * 3.40760841847;
  312. t31 = s[15][ss] + s[16][ss]; t32 = (s[15][ss] - s[16][ss]) * 10.1900081235;
  313. t33 = t01 + t31; t31 = (t01 - t31) * 0.502419286188;
  314. t01 = t03 + t29; t29 = (t03 - t29) * 0.52249861494;
  315. t03 = t05 + t27; t27 = (t05 - t27) * 0.566944034816;
  316. t05 = t07 + t25; t25 = (t07 - t25) * 0.64682178336;
  317. t07 = t09 + t23; t23 = (t09 - t23) * 0.788154623451;
  318. t09 = t11 + t21; t21 = (t11 - t21) * 1.06067768599;
  319. t11 = t13 + t19; t19 = (t13 - t19) * 1.72244709824;
  320. t13 = t15 + t17; t17 = (t15 - t17) * 5.10114861869;
  321. t15 = t33 + t13; t13 = (t33 - t13) * 0.509795579104;
  322. t33 = t01 + t11; t01 = (t01 - t11) * 0.601344886935;
  323. t11 = t03 + t09; t09 = (t03 - t09) * 0.899976223136;
  324. t03 = t05 + t07; t07 = (t05 - t07) * 2.56291544774;
  325. t05 = t15 + t03; t15 = (t15 - t03) * 0.541196100146;
  326. t03 = t33 + t11; t11 = (t33 - t11) * 1.30656296488;
  327. t33 = t05 + t03; t05 = (t05 - t03) * 0.707106781187;
  328. t03 = t15 + t11; t15 = (t15 - t11) * 0.707106781187;
  329. t03 += t15;
  330. t11 = t13 + t07; t13 = (t13 - t07) * 0.541196100146;
  331. t07 = t01 + t09; t09 = (t01 - t09) * 1.30656296488;
  332. t01 = t11 + t07; t07 = (t11 - t07) * 0.707106781187;
  333. t11 = t13 + t09; t13 = (t13 - t09) * 0.707106781187;
  334. t11 += t13; t01 += t11;
  335. t11 += t07; t07 += t13;
  336. t09 = t31 + t17; t31 = (t31 - t17) * 0.509795579104;
  337. t17 = t29 + t19; t29 = (t29 - t19) * 0.601344886935;
  338. t19 = t27 + t21; t21 = (t27 - t21) * 0.899976223136;
  339. t27 = t25 + t23; t23 = (t25 - t23) * 2.56291544774;
  340. t25 = t09 + t27; t09 = (t09 - t27) * 0.541196100146;
  341. t27 = t17 + t19; t19 = (t17 - t19) * 1.30656296488;
  342. t17 = t25 + t27; t27 = (t25 - t27) * 0.707106781187;
  343. t25 = t09 + t19; t19 = (t09 - t19) * 0.707106781187;
  344. t25 += t19;
  345. t09 = t31 + t23; t31 = (t31 - t23) * 0.541196100146;
  346. t23 = t29 + t21; t21 = (t29 - t21) * 1.30656296488;
  347. t29 = t09 + t23; t23 = (t09 - t23) * 0.707106781187;
  348. t09 = t31 + t21; t31 = (t31 - t21) * 0.707106781187;
  349. t09 += t31; t29 += t09; t09 += t23; t23 += t31;
  350. t17 += t29; t29 += t25; t25 += t09; t09 += t27;
  351. t27 += t23; t23 += t19; t19 += t31;
  352. t21 = t02 + t32; t02 = (t02 - t32) * 0.502419286188;
  353. t32 = t04 + t30; t04 = (t04 - t30) * 0.52249861494;
  354. t30 = t06 + t28; t28 = (t06 - t28) * 0.566944034816;
  355. t06 = t08 + t26; t08 = (t08 - t26) * 0.64682178336;
  356. t26 = t10 + t24; t10 = (t10 - t24) * 0.788154623451;
  357. t24 = t12 + t22; t22 = (t12 - t22) * 1.06067768599;
  358. t12 = t14 + t20; t20 = (t14 - t20) * 1.72244709824;
  359. t14 = t16 + t18; t16 = (t16 - t18) * 5.10114861869;
  360. t18 = t21 + t14; t14 = (t21 - t14) * 0.509795579104;
  361. t21 = t32 + t12; t32 = (t32 - t12) * 0.601344886935;
  362. t12 = t30 + t24; t24 = (t30 - t24) * 0.899976223136;
  363. t30 = t06 + t26; t26 = (t06 - t26) * 2.56291544774;
  364. t06 = t18 + t30; t18 = (t18 - t30) * 0.541196100146;
  365. t30 = t21 + t12; t12 = (t21 - t12) * 1.30656296488;
  366. t21 = t06 + t30; t30 = (t06 - t30) * 0.707106781187;
  367. t06 = t18 + t12; t12 = (t18 - t12) * 0.707106781187;
  368. t06 += t12;
  369. t18 = t14 + t26; t26 = (t14 - t26) * 0.541196100146;
  370. t14 = t32 + t24; t24 = (t32 - t24) * 1.30656296488;
  371. t32 = t18 + t14; t14 = (t18 - t14) * 0.707106781187;
  372. t18 = t26 + t24; t24 = (t26 - t24) * 0.707106781187;
  373. t18 += t24; t32 += t18;
  374. t18 += t14; t26 = t14 + t24;
  375. t14 = t02 + t16; t02 = (t02 - t16) * 0.509795579104;
  376. t16 = t04 + t20; t04 = (t04 - t20) * 0.601344886935;
  377. t20 = t28 + t22; t22 = (t28 - t22) * 0.899976223136;
  378. t28 = t08 + t10; t10 = (t08 - t10) * 2.56291544774;
  379. t08 = t14 + t28; t14 = (t14 - t28) * 0.541196100146;
  380. t28 = t16 + t20; t20 = (t16 - t20) * 1.30656296488;
  381. t16 = t08 + t28; t28 = (t08 - t28) * 0.707106781187;
  382. t08 = t14 + t20; t20 = (t14 - t20) * 0.707106781187;
  383. t08 += t20;
  384. t14 = t02 + t10; t02 = (t02 - t10) * 0.541196100146;
  385. t10 = t04 + t22; t22 = (t04 - t22) * 1.30656296488;
  386. t04 = t14 + t10; t10 = (t14 - t10) * 0.707106781187;
  387. t14 = t02 + t22; t02 = (t02 - t22) * 0.707106781187;
  388. t14 += t02; t04 += t14; t14 += t10; t10 += t02;
  389. t16 += t04; t04 += t08; t08 += t14; t14 += t28;
  390. t28 += t10; t10 += t20; t20 += t02; t21 += t16;
  391. t16 += t32; t32 += t04; t04 += t06; t06 += t08;
  392. t08 += t18; t18 += t14; t14 += t30; t30 += t28;
  393. t28 += t26; t26 += t10; t10 += t12; t12 += t20;
  394. t20 += t24; t24 += t02;
  395. d[dp + 48] = -t33;
  396. d[dp + 49] = d[dp + 47] = -t21;
  397. d[dp + 50] = d[dp + 46] = -t17;
  398. d[dp + 51] = d[dp + 45] = -t16;
  399. d[dp + 52] = d[dp + 44] = -t01;
  400. d[dp + 53] = d[dp + 43] = -t32;
  401. d[dp + 54] = d[dp + 42] = -t29;
  402. d[dp + 55] = d[dp + 41] = -t04;
  403. d[dp + 56] = d[dp + 40] = -t03;
  404. d[dp + 57] = d[dp + 39] = -t06;
  405. d[dp + 58] = d[dp + 38] = -t25;
  406. d[dp + 59] = d[dp + 37] = -t08;
  407. d[dp + 60] = d[dp + 36] = -t11;
  408. d[dp + 61] = d[dp + 35] = -t18;
  409. d[dp + 62] = d[dp + 34] = -t09;
  410. d[dp + 63] = d[dp + 33] = -t14;
  411. d[dp + 32] = -t05;
  412. d[dp + 0] = t05; d[dp + 31] = -t30;
  413. d[dp + 1] = t30; d[dp + 30] = -t27;
  414. d[dp + 2] = t27; d[dp + 29] = -t28;
  415. d[dp + 3] = t28; d[dp + 28] = -t07;
  416. d[dp + 4] = t07; d[dp + 27] = -t26;
  417. d[dp + 5] = t26; d[dp + 26] = -t23;
  418. d[dp + 6] = t23; d[dp + 25] = -t10;
  419. d[dp + 7] = t10; d[dp + 24] = -t15;
  420. d[dp + 8] = t15; d[dp + 23] = -t12;
  421. d[dp + 9] = t12; d[dp + 22] = -t19;
  422. d[dp + 10] = t19; d[dp + 21] = -t20;
  423. d[dp + 11] = t20; d[dp + 20] = -t13;
  424. d[dp + 12] = t13; d[dp + 19] = -t24;
  425. d[dp + 13] = t24; d[dp + 18] = -t31;
  426. d[dp + 14] = t31; d[dp + 17] = -t02;
  427. d[dp + 15] = t02; d[dp + 16] = 0.0;
  428. };
  429. MP2.FRAME_SYNC = 0x7ff;
  430. MP2.VERSION = {
  431. MPEG_2_5: 0x0,
  432. MPEG_2: 0x2,
  433. MPEG_1: 0x3
  434. };
  435. MP2.LAYER = {
  436. III: 0x1,
  437. II: 0x2,
  438. I: 0x3
  439. };
  440. MP2.MODE = {
  441. STEREO: 0x0,
  442. JOINT_STEREO: 0x1,
  443. DUAL_CHANNEL: 0x2,
  444. MONO: 0x3
  445. };
  446. MP2.SAMPLE_RATE = new Uint16Array([
  447. 44100, 48000, 32000, 0, // MPEG-1
  448. 22050, 24000, 16000, 0 // MPEG-2
  449. ]);
  450. MP2.BIT_RATE = new Uint16Array([
  451. 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, // MPEG-1
  452. 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 // MPEG-2
  453. ]);
  454. MP2.SCALEFACTOR_BASE = new Uint32Array([
  455. 0x02000000, 0x01965FEA, 0x01428A30
  456. ]);
  457. MP2.SYNTHESIS_WINDOW = new Float32Array([
  458. 0.0, -0.5, -0.5, -0.5, -0.5, -0.5,
  459. -0.5, -1.0, -1.0, -1.0, -1.0, -1.5,
  460. -1.5, -2.0, -2.0, -2.5, -2.5, -3.0,
  461. -3.5, -3.5, -4.0, -4.5, -5.0, -5.5,
  462. -6.5, -7.0, -8.0, -8.5, -9.5, -10.5,
  463. -12.0, -13.0, -14.5, -15.5, -17.5, -19.0,
  464. -20.5, -22.5, -24.5, -26.5, -29.0, -31.5,
  465. -34.0, -36.5, -39.5, -42.5, -45.5, -48.5,
  466. -52.0, -55.5, -58.5, -62.5, -66.0, -69.5,
  467. -73.5, -77.0, -80.5, -84.5, -88.0, -91.5,
  468. -95.0, -98.0, -101.0, -104.0, 106.5, 109.0,
  469. 111.0, 112.5, 113.5, 114.0, 114.0, 113.5,
  470. 112.0, 110.5, 107.5, 104.0, 100.0, 94.5,
  471. 88.5, 81.5, 73.0, 63.5, 53.0, 41.5,
  472. 28.5, 14.5, -1.0, -18.0, -36.0, -55.5,
  473. -76.5, -98.5, -122.0, -147.0, -173.5, -200.5,
  474. -229.5, -259.5, -290.5, -322.5, -355.5, -389.5,
  475. -424.0, -459.5, -495.5, -532.0, -568.5, -605.0,
  476. -641.5, -678.0, -714.0, -749.0, -783.5, -817.0,
  477. -849.0, -879.5, -908.5, -935.0, -959.5, -981.0,
  478. -1000.5, -1016.0, -1028.5, -1037.5, -1042.5, -1043.5,
  479. -1040.0, -1031.5, 1018.5, 1000.0, 976.0, 946.5,
  480. 911.0, 869.5, 822.0, 767.5, 707.0, 640.0,
  481. 565.5, 485.0, 397.0, 302.5, 201.0, 92.5,
  482. -22.5, -144.0, -272.5, -407.0, -547.5, -694.0,
  483. -846.0, -1003.0, -1165.0, -1331.5, -1502.0, -1675.5,
  484. -1852.5, -2031.5, -2212.5, -2394.0, -2576.5, -2758.5,
  485. -2939.5, -3118.5, -3294.5, -3467.5, -3635.5, -3798.5,
  486. -3955.0, -4104.5, -4245.5, -4377.5, -4499.0, -4609.5,
  487. -4708.0, -4792.5, -4863.5, -4919.0, -4958.0, -4979.5,
  488. -4983.0, -4967.5, -4931.5, -4875.0, -4796.0, -4694.5,
  489. -4569.5, -4420.0, -4246.0, -4046.0, -3820.0, -3567.0,
  490. 3287.0, 2979.5, 2644.0, 2280.5, 1888.0, 1467.5,
  491. 1018.5, 541.0, 35.0, -499.0, -1061.0, -1650.0,
  492. -2266.5, -2909.0, -3577.0, -4270.0, -4987.5, -5727.5,
  493. -6490.0, -7274.0, -8077.5, -8899.5, -9739.0, -10594.5,
  494. -11464.5, -12347.0, -13241.0, -14144.5, -15056.0, -15973.5,
  495. -16895.5, -17820.0, -18744.5, -19668.0, -20588.0, -21503.0,
  496. -22410.5, -23308.5, -24195.0, -25068.5, -25926.5, -26767.0,
  497. -27589.0, -28389.0, -29166.5, -29919.0, -30644.5, -31342.0,
  498. -32009.5, -32645.0, -33247.0, -33814.5, -34346.0, -34839.5,
  499. -35295.0, -35710.0, -36084.5, -36417.5, -36707.5, -36954.0,
  500. -37156.5, -37315.0, -37428.0, -37496.0, 37519.0, 37496.0,
  501. 37428.0, 37315.0, 37156.5, 36954.0, 36707.5, 36417.5,
  502. 36084.5, 35710.0, 35295.0, 34839.5, 34346.0, 33814.5,
  503. 33247.0, 32645.0, 32009.5, 31342.0, 30644.5, 29919.0,
  504. 29166.5, 28389.0, 27589.0, 26767.0, 25926.5, 25068.5,
  505. 24195.0, 23308.5, 22410.5, 21503.0, 20588.0, 19668.0,
  506. 18744.5, 17820.0, 16895.5, 15973.5, 15056.0, 14144.5,
  507. 13241.0, 12347.0, 11464.5, 10594.5, 9739.0, 8899.5,
  508. 8077.5, 7274.0, 6490.0, 5727.5, 4987.5, 4270.0,
  509. 3577.0, 2909.0, 2266.5, 1650.0, 1061.0, 499.0,
  510. -35.0, -541.0, -1018.5, -1467.5, -1888.0, -2280.5,
  511. -2644.0, -2979.5, 3287.0, 3567.0, 3820.0, 4046.0,
  512. 4246.0, 4420.0, 4569.5, 4694.5, 4796.0, 4875.0,
  513. 4931.5, 4967.5, 4983.0, 4979.5, 4958.0, 4919.0,
  514. 4863.5, 4792.5, 4708.0, 4609.5, 4499.0, 4377.5,
  515. 4245.5, 4104.5, 3955.0, 3798.5, 3635.5, 3467.5,
  516. 3294.5, 3118.5, 2939.5, 2758.5, 2576.5, 2394.0,
  517. 2212.5, 2031.5, 1852.5, 1675.5, 1502.0, 1331.5,
  518. 1165.0, 1003.0, 846.0, 694.0, 547.5, 407.0,
  519. 272.5, 144.0, 22.5, -92.5, -201.0, -302.5,
  520. -397.0, -485.0, -565.5, -640.0, -707.0, -767.5,
  521. -822.0, -869.5, -911.0, -946.5, -976.0, -1000.0,
  522. 1018.5, 1031.5, 1040.0, 1043.5, 1042.5, 1037.5,
  523. 1028.5, 1016.0, 1000.5, 981.0, 959.5, 935.0,
  524. 908.5, 879.5, 849.0, 817.0, 783.5, 749.0,
  525. 714.0, 678.0, 641.5, 605.0, 568.5, 532.0,
  526. 495.5, 459.5, 424.0, 389.5, 355.5, 322.5,
  527. 290.5, 259.5, 229.5, 200.5, 173.5, 147.0,
  528. 122.0, 98.5, 76.5, 55.5, 36.0, 18.0,
  529. 1.0, -14.5, -28.5, -41.5, -53.0, -63.5,
  530. -73.0, -81.5, -88.5, -94.5, -100.0, -104.0,
  531. -107.5, -110.5, -112.0, -113.5, -114.0, -114.0,
  532. -113.5, -112.5, -111.0, -109.0, 106.5, 104.0,
  533. 101.0, 98.0, 95.0, 91.5, 88.0, 84.5,
  534. 80.5, 77.0, 73.5, 69.5, 66.0, 62.5,
  535. 58.5, 55.5, 52.0, 48.5, 45.5, 42.5,
  536. 39.5, 36.5, 34.0, 31.5, 29.0, 26.5,
  537. 24.5, 22.5, 20.5, 19.0, 17.5, 15.5,
  538. 14.5, 13.0, 12.0, 10.5, 9.5, 8.5,
  539. 8.0, 7.0, 6.5, 5.5, 5.0, 4.5,
  540. 4.0, 3.5, 3.5, 3.0, 2.5, 2.5,
  541. 2.0, 2.0, 1.5, 1.5, 1.0, 1.0,
  542. 1.0, 1.0, 0.5, 0.5, 0.5, 0.5,
  543. 0.5, 0.5
  544. ]);
  545. // Quantizer lookup, step 1: bitrate classes
  546. MP2.QUANT_LUT_STEP_1 = [
  547. // 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384 <- bitrate
  548. [ 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2], // mono
  549. // 16, 24, 28, 32, 40, 48, 56, 64, 80, 96,112,128,160,192 <- bitrate / chan
  550. [ 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2] // stereo
  551. ];
  552. // Quantizer lookup, step 2: bitrate class, sample rate -> B2 table idx, sblimit
  553. MP2.QUANT_TAB = {
  554. A: (27 | 64), // Table 3-B.2a: high-rate, sblimit = 27
  555. B: (30 | 64), // Table 3-B.2b: high-rate, sblimit = 30
  556. C: 8, // Table 3-B.2c: low-rate, sblimit = 8
  557. D: 12 // Table 3-B.2d: low-rate, sblimit = 12
  558. };
  559. MP2.QUANT_LUT_STEP_2 = [
  560. // 44.1 kHz, 48 kHz, 32 kHz
  561. [MP2.QUANT_TAB.C, MP2.QUANT_TAB.C, MP2.QUANT_TAB.D], // 32 - 48 kbit/sec/ch
  562. [MP2.QUANT_TAB.A, MP2.QUANT_TAB.A, MP2.QUANT_TAB.A], // 56 - 80 kbit/sec/ch
  563. [MP2.QUANT_TAB.B, MP2.QUANT_TAB.A, MP2.QUANT_TAB.B] // 96+ kbit/sec/ch
  564. ];
  565. // Quantizer lookup, step 3: B2 table, subband -> nbal, row index
  566. // (upper 4 bits: nbal, lower 4 bits: row index)
  567. MP2.QUANT_LUT_STEP_3 = [
  568. // Low-rate table (3-B.2c and 3-B.2d)
  569. [
  570. 0x44,0x44,
  571. 0x34,0x34,0x34,0x34,0x34,0x34,0x34,0x34,0x34,0x34
  572. ],
  573. // High-rate table (3-B.2a and 3-B.2b)
  574. [
  575. 0x43,0x43,0x43,
  576. 0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
  577. 0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,
  578. 0x20,0x20,0x20,0x20,0x20,0x20,0x20
  579. ],
  580. // MPEG-2 LSR table (B.2 in ISO 13818-3)
  581. [
  582. 0x45,0x45,0x45,0x45,
  583. 0x34,0x34,0x34,0x34,0x34,0x34,0x34,
  584. 0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,
  585. 0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24
  586. ]
  587. ];
  588. // Quantizer lookup, step 4: table row, allocation[] value -> quant table index
  589. MP2.QUANT_LUT_STEP4 = [
  590. [0, 1, 2, 17],
  591. [0, 1, 2, 3, 4, 5, 6, 17],
  592. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17],
  593. [0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
  594. [0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17],
  595. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  596. ];
  597. MP2.QUANT_TAB = [
  598. {levels: 3, group: 1, bits: 5}, // 1
  599. {levels: 5, group: 1, bits: 7}, // 2
  600. {levels: 7, group: 0, bits: 3}, // 3
  601. {levels: 9, group: 1, bits: 10}, // 4
  602. {levels: 15, group: 0, bits: 4}, // 5
  603. {levels: 31, group: 0, bits: 5}, // 6
  604. {levels: 63, group: 0, bits: 6}, // 7
  605. {levels: 127, group: 0, bits: 7}, // 8
  606. {levels: 255, group: 0, bits: 8}, // 9
  607. {levels: 511, group: 0, bits: 9}, // 10
  608. {levels: 1023, group: 0, bits: 10}, // 11
  609. {levels: 2047, group: 0, bits: 11}, // 12
  610. {levels: 4095, group: 0, bits: 12}, // 13
  611. {levels: 8191, group: 0, bits: 13}, // 14
  612. {levels: 16383, group: 0, bits: 14}, // 15
  613. {levels: 32767, group: 0, bits: 15}, // 16
  614. {levels: 65535, group: 0, bits: 16} // 17
  615. ];
  616. return MP2;
  617. })();