PageRenderTime 43ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/decoders/audio/mp3.d

http://github.com/wilkie/djehuty
D | 2440 lines | 342 code | 117 blank | 1981 comment | 62 complexity | fa55c9c98c9834a83ed3bbd9f7d0c65d MD5 | raw file
  1. /*
  2. * mp3.d
  3. *
  4. * This file implements the MP3 audio standard.
  5. *
  6. * Author: Dave Wilkinson
  7. *
  8. */
  9. module decoders.audio.mp3;
  10. import decoders.audio.decoder;
  11. import decoders.decoder;
  12. import core.stream;
  13. import core.time;
  14. import core.string;
  15. import core.definitions;
  16. import io.wavelet;
  17. import io.audio;
  18. import io.console;
  19. import math.common;
  20. // initialize the array to zero
  21. typedef double zerodouble = 0.0;
  22. template FromBigEndian(uint input) {
  23. version (LittleEndian) {
  24. const auto FromBigEndian = (input >> 24) | ((input >> 8) & 0x0000FF00) | ((input << 8) & 0x00FF0000) | ((input << 24) & 0xFF000000);
  25. }
  26. else {
  27. const auto FromBigEndian = input;
  28. }
  29. }
  30. template FromBigEndianBitIndex32(uint index) {
  31. version (LittleEndian) {
  32. const auto FromBigEndianBitIndex32 = ((3 - cast(uint)(index/8)) * 8) + (index % 8);
  33. }
  34. else {
  35. const auto FromBigEndianBitIndex32 = index;
  36. }
  37. }
  38. class MP3Decoder : AudioDecoder {
  39. private:
  40. enum : uint {
  41. MP3_STATE_INIT,
  42. MP3_BUFFER_AUDIO,
  43. MP3_READ_HEADER,
  44. MP3_AMBIGUOUS_SYNC,
  45. MP3_READ_CRC,
  46. MP3_READ_AUDIO_DATA,
  47. MP3_READ_AUDIO_DATA_PREAMBLE,
  48. MP3_READ_AUDIO_DATA_SINGLE_CHANNEL,
  49. MP3_READ_AUDIO_DATA_SCALE_FACTORS,
  50. MP3_READ_AUDIO_DATA_JOINT_STEREO,
  51. }
  52. align(1) struct MP3HeaderInformation {
  53. uint ID;
  54. uint Layer;
  55. uint Protected;
  56. uint BitrateIndex;
  57. uint SamplingFrequency;
  58. uint Padding;
  59. uint Private;
  60. uint Mode;
  61. uint ModeExtension;
  62. uint Copyright;
  63. uint Original;
  64. uint Emphasis;
  65. }
  66. align(1) struct ID3HeaderInformation {
  67. ubyte[3] signature;
  68. ubyte[2] ver;
  69. ubyte flags;
  70. ubyte[4] len;
  71. }
  72. const auto MPEG_SYNC_BITS = FromBigEndian!(0xFFF00000);
  73. const auto MPEG_ID_BIT = FromBigEndian!(0x00080000);
  74. const auto MPEG_LAYER = FromBigEndian!(0x00060000);
  75. const auto MPEG_LAYER_SHIFT = FromBigEndianBitIndex32!(17);
  76. const auto MPEG_PROTECTION_BIT = FromBigEndian!(0x00010000);
  77. const auto MPEG_BITRATE_INDEX = FromBigEndian!(0x0000F000);
  78. const auto MPEG_BITRATE_INDEX_SHIFT = FromBigEndianBitIndex32!(12);
  79. const auto MPEG_SAMPLING_FREQ = FromBigEndian!(0x00000C00);
  80. const auto MPEG_SAMPLING_FREQ_SHIFT = FromBigEndianBitIndex32!(10);
  81. const auto MPEG_PADDING_BIT = FromBigEndian!(0x00000200);
  82. const auto MPEG_PRIVATE_BIT = FromBigEndian!(0x00000100);
  83. const auto MPEG_MODE = FromBigEndian!(0x000000C0);
  84. const auto MPEG_MODE_SHIFT = FromBigEndianBitIndex32!(6);
  85. const auto MPEG_MODE_EXTENSION = FromBigEndian!(0x00000030);
  86. const auto MPEG_MODE_EXTENSION_SHIFT = FromBigEndianBitIndex32!(4);
  87. const auto MPEG_COPYRIGHT = FromBigEndian!(0x00000008);
  88. const auto MPEG_ORIGINAL = FromBigEndian!(0x00000004);
  89. const auto MPEG_EMPHASIS = FromBigEndian!(0x00000003);
  90. const auto MPEG_EMPHASIS_SHIFT = 0;
  91. // modes
  92. const auto MPEG_MODE_STEREO = 0;
  93. const auto MPEG_MODE_JOINT_STEREO = 1;
  94. const auto MPEG_MODE_DUAL_CHANNEL = 2;
  95. const auto MPEG_MODE_SINGLE_CHANNEL = 3;
  96. const uint byteMasks[9][] = [
  97. [0x00, 0x00, 0x00, 0x00, 0x0, 0x0, 0x0, 0x0], // 0 bit
  98. [0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1], // 1 bit
  99. [0xC0, 0x60, 0x30, 0x18, 0xC, 0x6, 0x3, 0x1], // 2 bits
  100. [0xE0, 0x70, 0x38, 0x1C, 0xE, 0x7, 0x3, 0x1], // 3 bits
  101. [0xF0, 0x78, 0x3C, 0x1E, 0xF, 0x7, 0x3, 0x1], // 4 bits
  102. [0xF8, 0x7C, 0x3E, 0x1F, 0xF, 0x7, 0x3, 0x1], // 5 bits
  103. [0xFC, 0x7E, 0x3F, 0x1F, 0xF, 0x7, 0x3, 0x1], // 6 bits
  104. [0xFE, 0x7F, 0x3F, 0x1F, 0xF, 0x7, 0x3, 0x1], // 7 bits
  105. [0xFF, 0x7F, 0x3F, 0x1F, 0xF, 0x7, 0x3, 0x1] // 8 bits
  106. ];
  107. // layer 3 bit rates (MPEG-1)
  108. const uint[] bitRates = [ 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 ]; // the first entry is the 'free' bitrate
  109. const double[] samplingFrequencies = [ 44.1, 48.0, 32.0, 1.0 ]; // the final entry is reserved, but set to 1.0 due to being used in division
  110. // Scalefactor Band Indices
  111. const uint[23][3] sfindex_long = [
  112. // 44.1
  113. [0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418, 576],
  114. // 48.0
  115. [0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384, 576],
  116. // 32.0
  117. [0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550, 576]
  118. ];
  119. const uint[14][3] sfindex_short = [
  120. // 44.1
  121. [0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192],
  122. // 48.0
  123. [0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192],
  124. // 32.0
  125. [0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192]
  126. ];
  127. // Synthesis Filter Working Area:
  128. int bufOffset[2] = [64,64];
  129. zerodouble BB[2][2*512];
  130. // Static Huffman tables
  131. // Imports huffmanTables[][][] and huffmanValues[][]
  132. // These are in an array from 0 - 16 and then table 24 being 17
  133. // Index as: huffmanTables[tableIndex][bitlength][value]
  134. // huffmanValues[tableIndex][value]
  135. // Both tables correspond in their order.
  136. import decoders.audio.mp3Huffman;
  137. uint[][] curTable;
  138. uint[] curValues;
  139. uint linbits;
  140. // Select the table to use for decoding and reset state.
  141. void initializeHuffman(uint region, uint gr, uint ch) {
  142. uint tableIndex = table_select[region][gr][ch];
  143. switch (tableIndex) {
  144. case 16:
  145. linbits = 1;
  146. break;
  147. case 17:
  148. linbits = 2;
  149. break;
  150. case 18:
  151. linbits = 3;
  152. break;
  153. case 24:
  154. case 19:
  155. linbits = 4;
  156. break;
  157. case 25:
  158. linbits = 5;
  159. break;
  160. case 26:
  161. case 20:
  162. linbits = 6;
  163. break;
  164. case 27:
  165. linbits = 7;
  166. break;
  167. case 28:
  168. case 21:
  169. linbits = 8;
  170. break;
  171. case 29:
  172. linbits = 9;
  173. break;
  174. case 22:
  175. linbits = 10;
  176. break;
  177. case 30:
  178. linbits = 11;
  179. break;
  180. case 31:
  181. case 23:
  182. linbits = 13;
  183. break;
  184. default:
  185. linbits = 0;
  186. break;
  187. }
  188. if (tableIndex >= 24) {
  189. tableIndex = 17;
  190. }
  191. else if (tableIndex >= 16) {
  192. tableIndex = 16;
  193. }
  194. // XXX: This silliness is due to a compiler bug in DMD 1.046
  195. if (tableIndex == 17) {
  196. curTable = huffmanTable24;
  197. }
  198. else {
  199. curTable = huffmanTables[tableIndex];
  200. }
  201. curValues = huffmanValues[tableIndex];
  202. }
  203. int[] readCode() {
  204. uint code;
  205. uint bitlength;
  206. uint valoffset;
  207. for(;;) {
  208. code <<= 1;
  209. code |= readBits(1);
  210. if (bitlength > curTable.length) {
  211. break;
  212. }
  213. foreach(uint i, foo; curTable[bitlength]) {
  214. if (foo == code) {
  215. // found code
  216. // get value offset
  217. valoffset += i;
  218. valoffset *= 2;
  219. int[] values = [curValues[valoffset], curValues[valoffset+1]];
  220. // Console.putln("b:", values[0], " ", values[1]);
  221. // read linbits (x)
  222. if (linbits > 0 && values[0] == 15) {
  223. values[0] += readBits(linbits);
  224. }
  225. if (values[0] > 0) {
  226. if (readBits(1) == 1) {
  227. values[0] = -values[0];
  228. }
  229. }
  230. if (linbits > 0 && values[1] == 15) {
  231. values[1] += readBits(linbits);
  232. }
  233. if (values[1] > 0) {
  234. if (readBits(1) == 1) {
  235. values[1] = -values[1];
  236. }
  237. }
  238. //Console.putln("a:", values[0], " ", values[1]);
  239. return values;
  240. }
  241. }
  242. valoffset += curTable[bitlength].length;
  243. bitlength++;
  244. if (bitlength >= curTable.length) {
  245. return [128, 128];
  246. }
  247. }
  248. return [128, 128];
  249. }
  250. uint curCountTable;
  251. void initializeQuantizationHuffman(uint gr, uint ch) {
  252. curCountTable = count1table_select[gr][ch];
  253. }
  254. int[] readQuantizationCode() {
  255. uint code = decodeQuantization();
  256. int v = ((code >> 3) & 1);
  257. int w = ((code >> 2) & 1);
  258. int x = ((code >> 1) & 1);
  259. int y = (code & 1);
  260. // Get Sign Bits (for non-zero values)
  261. if (v > 0 && readBits(1) == 1) {
  262. v = -v;
  263. }
  264. if (w > 0 && readBits(1) == 1) {
  265. w = -w;
  266. }
  267. if (x > 0 && readBits(1) == 1) {
  268. x = -x;
  269. }
  270. if (y > 0 && readBits(1) == 1) {
  271. y = -y;
  272. }
  273. // Console.putln("v: ", v, " w: ", w, " x: ", x , " y: ",y );
  274. return [v,w,x,y];
  275. }
  276. uint decodeQuantization() {
  277. uint code;
  278. if (curCountTable == 1) {
  279. // Quantization Huffman Table B is trivial...
  280. // It is simply the bitwise negation of 4 bits from the stream.
  281. // code = ~readBits(4);
  282. code = readBits(4);
  283. code = ~code;
  284. }
  285. else {
  286. // Quantization Huffman Table A is the only case,
  287. // so it is written here by hand:
  288. // state 1
  289. code = readBits(1);
  290. if (code == 0b1) {
  291. return 0b0000;
  292. }
  293. // state 2
  294. code = readBits(3);
  295. if (code >= 0b0100 && code <= 0b0111) {
  296. uint idx = code - 0b0100;
  297. const uint[] stage2_values = [0b0010, 0b0101, 0b0110, 0b0111];
  298. return stage2_values[idx];
  299. }
  300. // state 3
  301. code <<= 1;
  302. code |= readBits(1);
  303. if (code >= 0b00011 && code <= 0b00111) {
  304. uint idx = code - 0b00011;
  305. const uint[] stage3_values = [0b1001, 0b0110, 0b0011, 0b1010, 0b1100];
  306. return stage3_values[idx];
  307. }
  308. // state 4
  309. code <<= 1;
  310. code |= readBits(1);
  311. if (code <= 0b000101) {
  312. const uint[] stage4_values = [0b1011, 0b1111, 0b1101, 0b1110, 0b0111, 0b0101];
  313. return stage4_values[code];
  314. }
  315. // invalid code;
  316. code = 0;
  317. }
  318. return code;
  319. }
  320. import decoders.audio.mpegCommon;
  321. bool accepted = false;
  322. // number of blocks (of 1728 samples) to buffer
  323. const auto NUM_BLOCKS = 40;
  324. Wavelet tempBuffer;
  325. public:
  326. string name() {
  327. return "MPEG Layer 3";
  328. }
  329. string extension() {
  330. return "mp3";
  331. }
  332. bool firstTime = true;
  333. StreamData decode(Stream stream, Wavelet toBuffer, ref AudioInfo wi) {
  334. for(;;) {
  335. switch (decoderState) {
  336. case MP3_STATE_INIT:
  337. decoderState = MP3_BUFFER_AUDIO;
  338. /* follow through */
  339. // attempts to find the 12-16 sync bits
  340. // if it is unsure of the filetype, it will
  341. // look for only 12 bits (b 1111 1111 1111)
  342. // if it knows its layer it will search for
  343. // the sync bits plus that part of the header
  344. case MP3_BUFFER_AUDIO:
  345. samplesLeft = 1728 * NUM_BLOCKS;
  346. if (toBuffer !is null) {
  347. samplesLeft = cast(int)(toBuffer.length() / 1728) * 1728;
  348. samplesLeft += 1728;
  349. bufferSize = samplesLeft;
  350. }
  351. bufferSize = samplesLeft;
  352. decoderState = MP3_READ_HEADER;
  353. // *** fall through *** //
  354. case MP3_READ_HEADER:
  355. //Console.putln("pos: ", new String("%x", stream.position));
  356. if (!stream.read(mpeg_header)) {
  357. return StreamData.Accepted;
  358. }
  359. decoderState = MP3_AMBIGUOUS_SYNC;
  360. // *** fall through *** //
  361. // look at the sync bits of the header
  362. // if they are not correct, shift the header
  363. // 8 bits and read another byte until the
  364. // sync bits match
  365. case MP3_AMBIGUOUS_SYNC:
  366. if ((mpeg_header & FromBigEndian!(0xFFFFFF00)) == FromBigEndian!(0x49443300)) {
  367. if (id3.signature[0] != 0x49) {
  368. if (!stream.read((cast(ubyte*)&id3) + 4, id3.sizeof - 4)) {
  369. return StreamData.Required;
  370. }
  371. id3.signature[0] = 0x49;
  372. id3.ver[0] = cast(ubyte)(mpeg_header & 0xFF);
  373. // skip the ID3 section
  374. foreach(b; id3.len) {
  375. id3length <<= 7;
  376. b &= 0x7f;
  377. id3length |= b;
  378. }
  379. //Console.putln("id3 length: ", new String("%x", id3length));
  380. }
  381. if (!stream.skip(id3length)) {
  382. return StreamData.Required;
  383. }
  384. decoderState = MP3_READ_HEADER;
  385. continue;
  386. }
  387. //Console.putln("mpeg_header ", new String("%x", mpeg_header) );
  388. if ((mpeg_header & MPEG_SYNC_BITS) == MPEG_SYNC_BITS) {
  389. // sync bits found
  390. //writeln("sync bits found ", stream.getPosition() - 4);
  391. // pull apart header
  392. // the header looks like this:
  393. // SYNCWORD 12 bits
  394. // ID 1 bit
  395. // LAYER 2 bits
  396. // PROTECTION BIT 1 bit
  397. // BITRATE INDEX 4 bits
  398. // SAMPLING FREQ 2 bits
  399. // PADDING BIT 1 bit
  400. // PRIVATE BIT 1 bit
  401. // MODE 2 bits
  402. // MODE EXTENSION 2 bits
  403. // COPYRIGHT 1 bit
  404. // ORIGINAL/HOME 1 bit
  405. // EMPHASIS 2 bits
  406. header.ID = (mpeg_header & MPEG_ID_BIT ? 1 : 0);
  407. header.Layer = (mpeg_header & MPEG_LAYER) >> MPEG_LAYER_SHIFT;
  408. if (header.Layer != 1) {
  409. return StreamData.Invalid;
  410. }
  411. header.Protected = (mpeg_header & MPEG_PROTECTION_BIT ? 1 : 0);
  412. header.BitrateIndex = (mpeg_header & MPEG_BITRATE_INDEX) >> MPEG_BITRATE_INDEX_SHIFT;
  413. header.SamplingFrequency = (mpeg_header & MPEG_SAMPLING_FREQ) >> MPEG_SAMPLING_FREQ_SHIFT;
  414. header.Padding = (mpeg_header & MPEG_PADDING_BIT ? 1 : 0);
  415. header.Private = (mpeg_header & MPEG_PRIVATE_BIT ? 1 : 0);
  416. header.Mode = (mpeg_header & MPEG_MODE) >> MPEG_MODE_SHIFT;
  417. header.ModeExtension = (mpeg_header & MPEG_MODE_EXTENSION) >> MPEG_MODE_EXTENSION_SHIFT;
  418. header.Copyright = (mpeg_header & MPEG_COPYRIGHT ? 1 : 0);
  419. header.Original = (mpeg_header & MPEG_ORIGINAL ? 1 : 0);
  420. header.Emphasis = (mpeg_header & MPEG_EMPHASIS) >> MPEG_EMPHASIS_SHIFT;
  421. /*Console.putln("Header: ", mpeg_header & MPEG_SYNC_BITS, "\n",
  422. "ID: ", header.ID, "\n",
  423. "Layer: ", header.Layer, "\n",
  424. "Protected: ", header.Protected, "\n",
  425. "BitrateIndex: ", header.BitrateIndex, "\n",
  426. "SamplingFrequency: ", header.SamplingFrequency, "\n",
  427. "Padding: ", header.Padding, "\n",
  428. "Private: ", header.Private, "\n",
  429. "Mode: ", header.Mode, "\n",
  430. "ModeExtension: ", header.ModeExtension, "\n",
  431. "Copyright: ", header.Copyright, "\n",
  432. "Original: ", header.Original, "\n",
  433. "Emphasis: ", header.Emphasis); //*/
  434. // Calculate the length of the Audio Data
  435. bufferLength = cast(uint)(144 * (cast(double)bitRates[header.BitrateIndex] / cast(double)samplingFrequencies[header.SamplingFrequency]));
  436. if (header.Padding) {
  437. bufferLength++;
  438. }
  439. // subtract the size of the header
  440. bufferLength -= 4;
  441. // set the format of the wave buffer
  442. if (header.SamplingFrequency == 0) {
  443. // 44.1 kHz
  444. wf.samplesPerSecond = 44100;
  445. }
  446. else if (header.SamplingFrequency == 1) {
  447. // 48 kHz
  448. wf.samplesPerSecond = 48000;
  449. }
  450. else {
  451. // 32 kHz
  452. wf.samplesPerSecond = 32000;
  453. }
  454. wf.compressionType = 1;
  455. switch(header.Mode) {
  456. case MPEG_MODE_STEREO:
  457. case MPEG_MODE_DUAL_CHANNEL:
  458. wf.numChannels = 2;
  459. break;
  460. case MPEG_MODE_SINGLE_CHANNEL:
  461. wf.numChannels = 1;
  462. break;
  463. case MPEG_MODE_JOINT_STEREO:
  464. wf.numChannels = 2;
  465. break;
  466. default: // impossible!
  467. return StreamData.Invalid;
  468. }
  469. wf.averageBytesPerSecond = wf.samplesPerSecond * 2 * wf.numChannels;
  470. wf.blockAlign = 2 * wf.numChannels;
  471. wf.bitsPerSample = 16;
  472. // set the wavelet's audio format
  473. if (toBuffer !is null) {
  474. toBuffer.setAudioFormat(wf);
  475. bufferTime = toBuffer.time;
  476. }
  477. if (header.Protected == 0) {
  478. decoderState = MP3_READ_CRC;
  479. }
  480. else {
  481. decoderState = MP3_READ_AUDIO_DATA;
  482. }
  483. if (!accepted) {
  484. if (toBuffer !is null) {
  485. if (toBuffer.length() != bufferSize) {
  486. //Console.putln("resize ", bufferSize, " from ", toBuffer.length());
  487. //toBuffer.resize(bufferSize);
  488. //Console.putln("resize ", bufferSize, " from ", toBuffer.length());
  489. }
  490. toBuffer.rewind();
  491. }
  492. if (toBuffer is null && isSeek == false) {
  493. decoderState = MP3_READ_AUDIO_DATA_PREAMBLE;
  494. return StreamData.Accepted;
  495. }
  496. }
  497. accepted = true;
  498. continue;
  499. }
  500. else {
  501. Console.putln("cur test ", mpeg_header & MPEG_SYNC_BITS, " @ ", stream.position - 4);
  502. ubyte curByte;
  503. // test 15K worth
  504. syncAmount++;
  505. if (syncAmount == 1024*15) {
  506. return StreamData.Invalid;
  507. }
  508. if (!stream.read(curByte)) {
  509. return StreamData.Required;
  510. }
  511. mpeg_header <<= 8;
  512. mpeg_header |= (cast(uint)curByte);
  513. }
  514. continue;
  515. case MP3_READ_CRC:
  516. if (!stream.read(crc)) {
  517. return StreamData.Required;
  518. }
  519. decoderState = MP3_READ_AUDIO_DATA;
  520. continue;
  521. case MP3_READ_AUDIO_DATA_PREAMBLE:
  522. if (toBuffer !is null) {
  523. samplesLeft = cast(int)(toBuffer.length() / 1728) * 1728;
  524. samplesLeft += 1728;
  525. bufferSize = samplesLeft;
  526. }
  527. decoderState = MP3_READ_AUDIO_DATA;
  528. /* FOLLOW THROUGH */
  529. case MP3_READ_AUDIO_DATA:
  530. // Console.putln("pos: ", new String("%x", stream.position));
  531. // curByte is currently at the end of the last frame (supposedly)
  532. // main_data_end depicts the end of the data frame
  533. curByte = audioData.length - bufferLength;
  534. curPos = 0;
  535. switch(header.Mode) {
  536. case MPEG_MODE_STEREO:
  537. case MPEG_MODE_DUAL_CHANNEL:
  538. channels = 2;
  539. decoderState = MP3_READ_AUDIO_DATA_SINGLE_CHANNEL;
  540. break;
  541. case MPEG_MODE_SINGLE_CHANNEL:
  542. channels = 1;
  543. decoderState = MP3_READ_AUDIO_DATA_SINGLE_CHANNEL;
  544. break;
  545. case MPEG_MODE_JOINT_STEREO:
  546. channels = 2;
  547. decoderState = MP3_READ_AUDIO_DATA_SINGLE_CHANNEL;
  548. break;
  549. default: // impossible!
  550. return StreamData.Invalid;
  551. }
  552. continue;
  553. case MP3_READ_AUDIO_DATA_SINGLE_CHANNEL:
  554. // Read in enough data for decoding a buffer
  555. audioHeaderLength = 32;
  556. if (channels == 1) { audioHeaderLength = 17; }
  557. //Console.putln("reading ", audioHeaderLength, " info header buffer");
  558. // read in the side info
  559. if (!stream.read(audioHeader.ptr, audioHeaderLength)) {
  560. return StreamData.Required;
  561. }
  562. // reset bit stream
  563. audioRef = audioHeader;
  564. audioRefLength = audioHeader.length;
  565. curByte = 0;
  566. curPos = 0;
  567. //Console.putln(audioRefLength, " <--- header length");
  568. // The reading of side info
  569. main_data_begin = readBits(9); // actually main_data_end in the spec,
  570. // but that makes no sense in this context
  571. if (channels == 1) {
  572. readBits(5); // ignore private_bits
  573. }
  574. else {
  575. readBits(3); // ignore private_bits
  576. }
  577. for(uint ch = 0; ch < channels; ch++) {
  578. for(uint scfsi_band = 0; scfsi_band < 4; scfsi_band++) {
  579. scfsi[scfsi_band][ch] = readBits(1);
  580. }
  581. }
  582. // 18 or 16 bits read
  583. for(uint gr=0; gr < 2; gr++) {
  584. for(uint ch = 0; ch < channels; ch++) {
  585. part2_3_length[gr][ch] = readBits(12);
  586. big_values[gr][ch] = readBits(9);
  587. global_gain[gr][ch] = readBits(8);
  588. scalefac_compress[gr][ch] = readBits(4);
  589. blocksplit_flag[gr][ch] = readBits(1);
  590. slen1[gr][ch] = slen1_interpret[scalefac_compress[gr][ch]];
  591. slen2[gr][ch] = slen2_interpret[scalefac_compress[gr][ch]];
  592. if (blocksplit_flag[gr][ch]) {
  593. block_type[gr][ch] = readBits(2);
  594. switch_point[gr][ch] = readBits(1);
  595. for (uint region = 0; region < 2; region++) {
  596. table_select[region][gr][ch] = readBits(5);
  597. }
  598. // window -- Number of actual time slot in case of
  599. // block_type == 2, 0 = window = 2.
  600. for (uint window = 0; window < 3; window++) {
  601. subblock_gain[window][gr][ch] = readBits(3);
  602. }
  603. if (block_type[gr][ch] == 2 && switch_point[gr][ch] == 0) {
  604. region_address1[gr][ch] = 8;
  605. }
  606. else {
  607. region_address1[gr][ch] = 7;
  608. }
  609. region_address2[gr][ch] = 20 - region_address1[gr][ch];
  610. if (switch_point[gr][ch] == 1) {
  611. switch_point_l[gr][ch] = 8;
  612. switch_point_s[gr][ch] = 3;
  613. }
  614. else {
  615. switch_point_l[gr][ch] = 0;
  616. switch_point_s[gr][ch] = 0;
  617. }
  618. }
  619. else {
  620. block_type[gr][ch] = 0;
  621. for (uint region = 0; region < 3; region++) {
  622. table_select[region][gr][ch] = readBits(5);
  623. }
  624. region_address1[gr][ch] = readBits(4);
  625. region_address2[gr][ch] = readBits(3);
  626. switch_point[gr][ch] = 0;
  627. switch_point_l[gr][ch] = 0;
  628. switch_point_s[gr][ch] = 0;
  629. }
  630. preflag[gr][ch] = readBits(1);
  631. scalefac_scale[gr][ch] = readBits(1);
  632. count1table_select[gr][ch] = readBits(1);
  633. }
  634. }
  635. bufferLength -= audioHeaderLength;
  636. //Console.putln("Audio Data Length: ", bufferLength);
  637. // append the buffer
  638. audioData ~= new ubyte[bufferLength];
  639. /* followed by main data (at main_data_end... not immediately following) */
  640. decoderState = MP3_READ_AUDIO_DATA_SCALE_FACTORS;
  641. case MP3_READ_AUDIO_DATA_SCALE_FACTORS:
  642. //Console.putln("reading ", bufferLength, " info decode buffer");
  643. // read in the data
  644. if (!stream.read(&audioData[$ - bufferLength], bufferLength)) {
  645. return StreamData.Required;
  646. }
  647. // reset bit stream
  648. curByte = audioData.length;
  649. curByte -= bufferLength;
  650. curByte -= main_data_begin;
  651. main_data_begin = curByte;
  652. //Console.putln(curByte, " start of read");
  653. audioRefLength = audioData.length;
  654. audioRef = audioData;
  655. curPos = 0;
  656. bool output = false;
  657. for (uint gr = 0; gr < 2; gr++) {
  658. //Console.putln("gr ", gr);
  659. for (uint ch = 0; ch < channels; ch++) {
  660. part2_length = (curByte * 8) + curPos;
  661. // Read the scalefactors for this granule
  662. readScalefactors(gr, ch);
  663. part2_length = ((curByte * 8) + curPos) - part2_length;
  664. // Decode the current huffman data
  665. decodeHuffman(gr, ch);
  666. // Requantize
  667. requantizeSample(gr, ch);
  668. // ro[ch] == quantizedData[ch]
  669. for (uint sb = 0; sb < SBLIMIT; sb++) {
  670. for (uint ss = 0; ss < SSLIMIT; ss++) {
  671. //printf("%f\n", quantizedData[ch][sb][ss]);
  672. }
  673. }
  674. }
  675. // Account for a mode switch from intensity stereo to MS_stereo
  676. normalizeStereo(gr);
  677. for (uint ch = 0; ch < channels; ch++) {
  678. for (uint sb = 0; sb < SBLIMIT; sb++) {
  679. for (uint ss = 0; ss < SSLIMIT; ss++) {
  680. //if (normalizedData[ch][sb][ss] > 0.0) {
  681. // printf("%f\n", normalizedData[ch][sb][ss]);
  682. //}
  683. }
  684. }
  685. }
  686. for (uint ch = 0; ch < channels; ch++) {
  687. // Reorder the short blocks
  688. reorder(gr, ch);
  689. for (uint sb = 0; sb < SBLIMIT; sb++) {
  690. for (uint ss = 0; ss < SSLIMIT; ss++) {
  691. //if (reorderedData[sb][ss] > 0.0) {
  692. //printf("%f\n", reorderedData[sb][ss]);
  693. //}
  694. }
  695. }
  696. // Perform anti-alias pass on subband butterflies
  697. antialias(gr, ch);
  698. for (uint sb = 0; sb < SBLIMIT; sb++) {
  699. for (uint ss = 0; ss < SSLIMIT; ss++) {
  700. // if (hybridData[sb][ss] > 0.0) {
  701. //printf("%f\n", hybridData[sb][ss]);
  702. // }
  703. }
  704. }
  705. // Perform hybrid synthesis pass
  706. for (uint sb; sb < SBLIMIT; sb++) {
  707. hybridSynthesis(gr, ch, sb);
  708. }
  709. // Multiply every second subband's every second input by -1
  710. // To correct for frequency inversion of the polyphase filterbank
  711. for (uint sb; sb < SBLIMIT; sb++) {
  712. for (uint ss; ss < SSLIMIT; ss++) {
  713. if (((ss % 2) == 1) && ((sb % 2) == 1)) {
  714. polysynthData[ch][sb][ss] = -polysynthData[ch][sb][ss];
  715. }
  716. }
  717. }
  718. for (uint sb = 0; sb < SBLIMIT; sb++) {
  719. for (uint ss = 0; ss < SSLIMIT; ss++) {
  720. //if (polysynthData[ch][sb][ss] > 0.0) {
  721. // printf("%f\n", polysynthData[ch][sb][ss]);
  722. //}
  723. }
  724. }
  725. }
  726. // Polyphase Synthesis
  727. //Console.putln("polyphase");
  728. for (uint ss; ss < 18; ss++) {
  729. polyphaseSynthesis(gr, ss, toBuffer);
  730. }
  731. //Console.putln("polyphase!");
  732. }
  733. samplesLeft -= (3*18*32*channels);
  734. if (samplesLeft <= 0) {
  735. decoderState = MP3_BUFFER_AUDIO;
  736. curTime += bufferTime;
  737. //curTime.toString();
  738. return StreamData.Accepted; /*
  739. toBuffer.rewind();
  740. continue; //*/
  741. }
  742. //Console.putln(curByte, " end of read");
  743. //Console.putln(curByte - main_data_begin, " read");
  744. main_data_end = curByte+1;
  745. /* followed by huffman encoded data */
  746. decoderState = MP3_READ_HEADER;
  747. continue;
  748. case MP3_READ_AUDIO_DATA_JOINT_STEREO:
  749. continue;
  750. default:
  751. break;
  752. }
  753. break;
  754. }
  755. return StreamData.Invalid;
  756. }
  757. protected:
  758. void readScalefactors(uint gr, uint ch) {
  759. if (blocksplit_flag[gr][ch] == 1 && block_type[gr][ch] == 2) {
  760. if (switch_point[gr][ch] == 0) {
  761. // Decoding scalefactors for a short window.
  762. for (uint cb = 0; cb < 6; cb++) {
  763. for (uint window = 0; window < 3; window++) {
  764. scalefac[gr][ch].short_window[cb][window] = readBits(slen1[gr][ch]);
  765. }
  766. }
  767. for (uint cb = 6; cb < cb_limit_short; cb++) {
  768. for (uint window = 0; window < 3; window++) {
  769. scalefac[gr][ch].short_window[cb][window] = readBits(slen2[gr][ch]);
  770. }
  771. }
  772. for (uint window = 0; window < 3; window++) {
  773. scalefac[gr][ch].short_window[12][window] = 0;
  774. }
  775. }
  776. else {
  777. // Decoding scalefactors for a long window with a switch point to short.
  778. for (uint cb = 0; cb < 8; cb++) {
  779. if ((scfsi[cb][ch] == 0) || (gr == 0)) {
  780. scalefac[gr][ch].long_window[cb] = readBits(slen1[gr][ch]);
  781. }
  782. }
  783. for (uint cb = 3; cb < 6; cb++) {
  784. for (uint window = 0; window < 3; window++) {
  785. if ((scfsi[cb][ch] == 0) || (gr == 0)) {
  786. scalefac[gr][ch].short_window[cb][window] = readBits(slen1[gr][ch]);
  787. }
  788. }
  789. }
  790. for (uint cb = 6; cb < cb_limit_short; cb++) {
  791. for (uint window = 0; window < 3; window++) {
  792. if ((scfsi[cb][ch] == 0) || (gr == 0)) {
  793. scalefac[gr][ch].short_window[cb][window] = readBits(slen2[gr][ch]);
  794. }
  795. }
  796. }
  797. for (uint window = 0; window < 3; window++) {
  798. if ((scfsi[cb_limit_short][ch] == 0) || (gr == 0)) {
  799. scalefac[gr][ch].short_window[cb_limit_short][window] = 0;
  800. }
  801. }
  802. }
  803. }
  804. else {
  805. // The block_type cannot be 2 in this block (so, it must be block 0, 1, or 3).
  806. // Decoding the scalefactors for a long window
  807. if ((scfsi[0][ch] == 0) || (gr == 0)) {
  808. for (uint cb = 0; cb < 6; cb++) {
  809. scalefac[gr][ch].long_window[cb] = readBits(slen1[gr][ch]);
  810. }
  811. }
  812. if ((scfsi[1][ch] == 0) || (gr == 0)) {
  813. for (uint cb = 6; cb < 11; cb++) {
  814. scalefac[gr][ch].long_window[cb] = readBits(slen1[gr][ch]);
  815. }
  816. }
  817. if ((scfsi[2][ch] == 0) || (gr == 0)) {
  818. for (uint cb = 11; cb < 16; cb++) {
  819. scalefac[gr][ch].long_window[cb] = readBits(slen2[gr][ch]);
  820. }
  821. }
  822. if ((scfsi[3][ch] == 0) || (gr == 0)) {
  823. for (uint cb = 16; cb < 21; cb++) {
  824. scalefac[gr][ch].long_window[cb] = readBits(slen2[gr][ch]);
  825. }
  826. }
  827. // (The reference implementation does nothing with subband 21)
  828. // We fill it with a high negative integer:
  829. scalefac[gr][ch].long_window[21] = -858993460;
  830. scalefac[gr][ch].long_window[22] = 0;
  831. }
  832. }
  833. const auto SBLIMIT = 32;
  834. const auto SSLIMIT = 18;
  835. // Decoded Huffman Data -- is(i) in the spec
  836. int[SSLIMIT][SBLIMIT] codedData;
  837. // Requantizated Data -- xr(i) in the spec
  838. double[SSLIMIT][SBLIMIT][2] quantizedData;
  839. // Normalized Data -- lr(i)
  840. double[SSLIMIT][SBLIMIT][2] normalizedData;
  841. // reordered data -- re(i)
  842. double[SSLIMIT][SBLIMIT] reorderedData;
  843. // anti-aliased hybrid synthesis data -- hybridIn
  844. double[SSLIMIT][SBLIMIT] hybridData;
  845. // data for the polysynth phase -- hybridOut
  846. double[SSLIMIT][SBLIMIT][2] polysynthData;
  847. void decodeHuffman(uint gr, uint ch) {
  848. // part2_3_length is the length of all of the data
  849. // (huffman + scalefactors). part2_length is just
  850. // the scalefactors by themselves.
  851. // Note: SSLIMIT * SBLIMIT = 32 * 18 = 576
  852. // Console.putln("-=-=-=-");
  853. static const auto max_table_entry = 15;
  854. uint region1Start;
  855. uint region2Start;
  856. if (blocksplit_flag[gr][ch] == 1 && block_type[gr][ch] == 2) {
  857. // Short Blocks
  858. region1Start = 36;
  859. region2Start = 576; // There isn't a region 2 for short blocks
  860. }
  861. else {
  862. // Long Blocks
  863. region1Start = sfindex_long[header.SamplingFrequency][region_address1[gr][ch] + 1];
  864. region2Start = sfindex_long[header.SamplingFrequency][region_address1[gr][ch] + region_address2[gr][ch] + 2];
  865. }
  866. uint maxBand = big_values[gr][ch] * 2;
  867. // Console.putln(region1Start, " to ", region2Start);
  868. if (region1Start > maxBand) { region1Start = maxBand; }
  869. if (region2Start > maxBand) { region2Start = maxBand; }
  870. uint freqIndex;
  871. uint pos = curByte;
  872. uint posbit = curPos;
  873. // The number of bits used for the huffman data
  874. uint huffmanLength = (part2_3_length[gr][ch] - part2_length);
  875. // The bit position in the stream to stop.
  876. uint maxBit = huffmanLength + curPos + (curByte * 8);
  877. // Region 0
  878. if (freqIndex < region1Start) {
  879. //Console.putln("region 0 -=-=-");
  880. initializeHuffman(0,gr,ch);
  881. }
  882. for (; freqIndex < region1Start; freqIndex+=2) {
  883. int[] code = readCode();
  884. codedData[freqIndex/SSLIMIT][freqIndex%SSLIMIT] = code[0];
  885. codedData[(freqIndex+1)/SSLIMIT][(freqIndex+1)%SSLIMIT] = code[1];
  886. }
  887. // Region 1
  888. if (freqIndex < region2Start) {
  889. // Console.putln("region 1 -=-=-");
  890. initializeHuffman(1,gr,ch);
  891. }
  892. for (; freqIndex < region2Start; freqIndex+=2) {
  893. int[] code = readCode();
  894. codedData[freqIndex/SSLIMIT][freqIndex%SSLIMIT] = code[0];
  895. codedData[(freqIndex+1)/SSLIMIT][(freqIndex+1)%SSLIMIT] = code[1];
  896. }
  897. // Region 2
  898. if (freqIndex < maxBand) {
  899. // Console.putln("region 2 -=-=-");
  900. initializeHuffman(2,gr,ch);
  901. }
  902. for (; freqIndex < maxBand; freqIndex+=2) {
  903. int[] code = readCode();
  904. codedData[freqIndex/SSLIMIT][freqIndex%SSLIMIT] = code[0];
  905. codedData[(freqIndex+1)/SSLIMIT][(freqIndex+1)%SSLIMIT] = code[1];
  906. }
  907. // Console.putln("big values decoded -=-=-");
  908. // Read in Count1 Area
  909. initializeQuantizationHuffman(gr,ch);
  910. for (; (curPos + (curByte * 8)) < maxBit && freqIndex < 574; freqIndex += 4) {
  911. int[4] code = readQuantizationCode();
  912. codedData[freqIndex/SSLIMIT][freqIndex%SSLIMIT] = code[0];
  913. codedData[(freqIndex+1)/SSLIMIT][(freqIndex+1)%SSLIMIT] = code[1];
  914. codedData[(freqIndex+2)/SSLIMIT][(freqIndex+2)%SSLIMIT] = code[2];
  915. codedData[(freqIndex+3)/SSLIMIT][(freqIndex+3)%SSLIMIT] = code[3];
  916. }
  917. // Zero rest
  918. for (; freqIndex < 576; freqIndex++) {
  919. codedData[freqIndex/SSLIMIT][freqIndex%SSLIMIT] = 0;
  920. }
  921. // Resync to the correct position
  922. // (where we started + the number of bits that would have been used)
  923. curByte = maxBit / 8;
  924. curPos = maxBit % 8;
  925. }
  926. void requantizeSample(uint gr, uint ch) {
  927. uint criticalBandBegin;
  928. uint criticalBandWidth;
  929. uint criticalBandBoundary;
  930. uint criticalBandIndex;
  931. const int[22] pretab = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0];
  932. // Initialize the critical boundary information
  933. if ((blocksplit_flag[gr][ch] == 1) && (block_type[gr][ch] == 2)) {
  934. if (switch_point[gr][ch] == 0) {
  935. // Short blocks
  936. criticalBandBoundary = sfindex_short[header.SamplingFrequency][1] * 3;
  937. criticalBandWidth = sfindex_short[header.SamplingFrequency][1];
  938. criticalBandBegin = 0;
  939. }
  940. else {
  941. // Long blocks come first for switched windows
  942. criticalBandBoundary = sfindex_long[header.SamplingFrequency][1];
  943. }
  944. }
  945. else {
  946. // Long windows
  947. criticalBandBoundary = sfindex_long[header.SamplingFrequency][1];
  948. }
  949. for (uint sb; sb < SBLIMIT; sb++) {
  950. for (uint ss; ss < SSLIMIT; ss++) {
  951. // Get the critical band boundary
  952. if ((sb * 18) + ss == criticalBandBoundary) {
  953. if (blocksplit_flag[gr][ch] == 1 && block_type[gr][ch] == 2) {
  954. if (switch_point[gr][ch] == 0) {
  955. // Requantizing the samples for a short window.
  956. criticalBandIndex++;
  957. criticalBandBoundary = sfindex_short[header.SamplingFrequency][criticalBandIndex+1]*3;
  958. criticalBandWidth = sfindex_short[header.SamplingFrequency][criticalBandIndex + 1] - sfindex_short[header.SamplingFrequency][criticalBandIndex];
  959. criticalBandBegin = sfindex_short[header.SamplingFrequency][criticalBandIndex] * 3;
  960. }
  961. else {
  962. // Requantizing the samples for a long window that switches to short.
  963. // The first two are long windows and the last two are short windows
  964. if (((sb * 18) + ss) == sfindex_long[header.SamplingFrequency][8]) {
  965. criticalBandBoundary = sfindex_short[header.SamplingFrequency][4] * 3;
  966. criticalBandIndex = 3;
  967. criticalBandWidth = sfindex_short[header.SamplingFrequency][criticalBandIndex + 1] - sfindex_short[header.SamplingFrequency][criticalBandIndex];
  968. criticalBandBegin = sfindex_short[header.SamplingFrequency][criticalBandIndex] * 3;
  969. }
  970. else if (((sb * 18) + ss) < sfindex_long[header.SamplingFrequency][8]) {
  971. criticalBandIndex++;
  972. criticalBandBoundary = sfindex_long[header.SamplingFrequency][criticalBandIndex+1];
  973. }
  974. else {
  975. criticalBandIndex++;
  976. criticalBandBoundary = sfindex_short[header.SamplingFrequency][criticalBandIndex + 1] * 3;
  977. criticalBandWidth = sfindex_short[header.SamplingFrequency][criticalBandIndex + 1] - sfindex_short[header.SamplingFrequency][criticalBandIndex];
  978. criticalBandBegin = sfindex_short[header.SamplingFrequency][criticalBandIndex] * 3;
  979. }
  980. }
  981. }
  982. else {
  983. // The block_type cannot be 2 in this block (so, it must be block 0, 1, or 3).
  984. // Requantizing the samples for a long window
  985. criticalBandIndex++;
  986. criticalBandBoundary = sfindex_long[header.SamplingFrequency][criticalBandIndex+1];
  987. }
  988. }
  989. // Global gain
  990. quantizedData[ch][sb][ss] = pow(2.0, (0.25 * (cast(double)global_gain[gr][ch] - 210.0)));
  991. //printf("g : %d %d: %f\n", sb,ss,quantizedData[ch][sb][ss]);
  992. static bool output = false;
  993. // Perform the scaling that depends on the type of window
  994. if (blocksplit_flag[gr][ch] == 1
  995. && (((block_type[gr][ch] == 2) && (switch_point[gr][ch] == 0))
  996. || ((block_type[gr][ch] == 2) && (switch_point[gr][ch] == 1) && (sb >= 2)))) {
  997. // Short blocks (either via block_type 2 or the last 2 bands for switched windows)
  998. uint sbgainIndex = (((sb * 18) + ss) - criticalBandBegin) / criticalBandWidth;
  999. // if (output) printf("%d %d\n", sbgainIndex, criticalBandIndex);
  1000. quantizedData[ch][sb][ss] *= pow(2.0, 0.25 * -8.0
  1001. * subblock_gain[sbgainIndex][gr][ch]);
  1002. quantizedData[ch][sb][ss] *= pow(2.0, 0.25 * -2.0 * (1.0 + scalefac_scale[gr][ch])
  1003. * scalefac[gr][ch].short_window[criticalBandIndex][sbgainIndex]);
  1004. }
  1005. else {
  1006. // Long blocks (either via block_type 0, 1, or 3, or the 1st 2 bands
  1007. double powExp = -0.5 * (1.0 + cast(double)scalefac_scale[gr][ch])
  1008. * (cast(double)scalefac[gr][ch].long_window[criticalBandIndex]
  1009. + (cast(double)preflag[gr][ch] * cast(double)pretab[criticalBandIndex]));
  1010. double powResult = pow(2.0, powExp);
  1011. //if (powResult > 0.0) {
  1012. //printf("r : %f\nfrom : %f\n", powResult, powExp);
  1013. //printf("with : %f %d [%d, %d, %d] %f %f\n", cast(double)scalefac_scale[gr][ch], scalefac[gr][ch].long_window[criticalBandIndex], gr, ch, criticalBandIndex,
  1014. // cast(double)preflag[gr][ch], cast(double)pretab[criticalBandIndex]);
  1015. //}
  1016. quantizedData[ch][sb][ss] *= powResult;
  1017. }
  1018. // Scale values
  1019. double powResult = pow(cast(double)abs(codedData[sb][ss]), 4.0/3.0);
  1020. // printf("%f\n", powResult);
  1021. quantizedData[ch][sb][ss] *= powResult;
  1022. if (codedData[sb][ss] < 0) {
  1023. quantizedData[ch][sb][ss] = -quantizedData[ch][sb][ss];
  1024. }
  1025. //printf("%d %d: [%d] %.21f\n", sb,ss, codedData[sb][ss], quantizedData[ch][sb][ss]);
  1026. }
  1027. }
  1028. }
  1029. void normalizeStereo(uint gr) {
  1030. double io;
  1031. if ((scalefac_compress[gr][0] % 2) == 1) {
  1032. io = 0.707106781188;
  1033. }
  1034. else {
  1035. io = 0.840896415256;
  1036. }
  1037. short[576] decodedPos;
  1038. double[576] decodedRatio;
  1039. double[576][2] k;
  1040. int i;
  1041. int sb;
  1042. int ss;
  1043. int ch;
  1044. int scalefactorBand;
  1045. // Initialize
  1046. decodedPos[0..$] = 7;
  1047. bool intensityStereo = (header.Mode == MPEG_MODE_JOINT_STEREO) && (header.ModeExtension & 0x1);
  1048. bool msStereo = (header.Mode == MPEG_MODE_JOINT_STEREO) && (header.ModeExtension & 0x2);
  1049. if ((channels == 2) && intensityStereo) {
  1050. if ((blocksplit_flag[gr][ch] == 1) && (block_type[gr][ch] == 2)) {
  1051. if (switch_point[gr][ch] == 0) {
  1052. for (uint j = 0; j < 3; j++) {
  1053. int scalefactorCount = -1;
  1054. for (scalefactorBand = 12; scalefactorBand >= 0; scalefactorBand--) {
  1055. int lines = sfindex_short[header.SamplingFrequency][scalefactorBand + 1]
  1056. - sfindex_short[header.SamplingFrequency][scalefactorBand];
  1057. i = 3 * sfindex_short[header.SamplingFrequency][scalefactorBand]
  1058. + ((j + 1) * lines) - 1;
  1059. for (; lines > 0; lines--) {
  1060. if (quantizedData[1][i/SSLIMIT][i%SSLIMIT] != 0.0) {
  1061. scalefactorCount = scalefactorBand;
  1062. scalefactorBand = -10;
  1063. lines = -10;
  1064. }
  1065. i--;
  1066. }
  1067. }
  1068. scalefactorBand = scalefactorCount + 1;
  1069. for (; scalefactorBand < 12; scalefactorBand++) {
  1070. sb = sfindex_short[header.SamplingFrequency][scalefactorBand+1]
  1071. - sfindex_short[header.SamplingFrequency][scalefactorBand];
  1072. i = 3 * sfindex_short[header.SamplingFrequency][scalefactorBand] + (j * sb);
  1073. for ( ; sb > 0; sb--) {
  1074. decodedPos[i] = cast(short)scalefac[gr][1].short_window[scalefactorBand][j];
  1075. if (decodedPos[i] != 7) {
  1076. // IF (MPEG2) { ... }
  1077. // ELSE {
  1078. decodedRatio[i] = tan(cast(double)decodedPos[i] * (PI / 12));
  1079. // }
  1080. }
  1081. i++;
  1082. }
  1083. }
  1084. sb = sfindex_short[header.SamplingFrequency][12] - sfindex_short[header.SamplingFrequency][11];
  1085. scalefactorBand = (3 * sfindex_short[header.SamplingFrequency][11]) + (j * sb);
  1086. sb = sfindex_short[header.SamplingFrequency][13] - sfindex_short[header.SamplingFrequency][12];
  1087. i = (3 * sfindex_short[header.SamplingFrequency][11]) + (j * sb);
  1088. for (; sb > 0; sb--) {
  1089. decodedPos[i] = decodedPos[scalefactorBand];
  1090. decodedRatio[i] = decodedRatio[scalefactorBand];
  1091. k[0][i] = k[0][scalefactorBand];
  1092. k[1][i] = k[1][scalefactorBand];
  1093. i++;
  1094. }
  1095. }
  1096. }
  1097. else {
  1098. int maxScalefactorBand;
  1099. for (uint j; j<3; j++) {
  1100. int scalefactorCount = 2;
  1101. for (scalefactorBand = 12; scalefactorBand >= 3; scalefactorBand--) {
  1102. int lines = sfindex_short[header.SamplingFrequency][scalefactorBand+1]
  1103. - sfindex_short[header.SamplingFrequency][scalefactorBand];
  1104. i = 3 * sfindex_short[header.SamplingFrequency][scalefactorBand]
  1105. + ((j + 1) * lines) - 1;
  1106. for (; lines > 0; lines--) {
  1107. if (quantizedData[1][i/SSLIMIT][i%SSLIMIT] != 0.0) {
  1108. scalefactorCount = scalefactorBand;
  1109. scalefactorBand = -10;
  1110. lines = -10;
  1111. }
  1112. i--;
  1113. }
  1114. }
  1115. scalefactorBand = scalefactorCount + 1;
  1116. if (scalefactorBand > maxScalefactorBand) {
  1117. maxScalefactorBand = scalefactorBand;
  1118. }
  1119. for (; scalefactorBand < 12; scalefactorBand++) {
  1120. sb = sfindex_short[header.SamplingFrequency][scalefactorBand+1]
  1121. - sfindex_short[header.SamplingFrequency][scalefactorBand];
  1122. i = 3 * sfindex_short[header.SamplingFrequency][scalefactorBand]
  1123. + (j * sb);
  1124. for (; sb > 0; sb--) {
  1125. decodedPos[i] = cast(short)scalefac[gr][1].short_window[scalefactorBand][j];
  1126. if (decodedPos[i] != 7) {
  1127. // IF (MPEG2) { ... }
  1128. // ELSE {
  1129. decodedRatio[i] = tan(cast(double)decodedPos[i] * (PI / 12.0));
  1130. // }
  1131. }
  1132. i++;
  1133. }
  1134. }
  1135. sb = sfindex_short[header.SamplingFrequency][12]
  1136. - sfindex_short[header.SamplingFrequency][11];
  1137. scalefactorBand = 3 * sfindex_short[header.SamplingFrequency][11]
  1138. + j * sb;
  1139. sb = sfindex_short[header.SamplingFrequency][13]
  1140. - sfindex_short[header.SamplingFrequency][12];
  1141. i = 3 * sfindex_short[header.SamplingFrequency][11] + j * sb;
  1142. for (; sb > 0; sb--) {
  1143. decodedPos[i] = decodedPos[scalefactorBand];
  1144. decodedRatio[i] = decodedRatio[scalefactorBand];
  1145. k[0][i] = k[0][scalefactorBand];
  1146. k[1][i] = k[1][scalefactorBand];
  1147. i++;
  1148. }
  1149. }
  1150. if (maxScalefactorBand <= 3) {
  1151. i = 2;
  1152. ss = 17;
  1153. sb = -1;
  1154. while (i >= 0) {
  1155. if (quantizedData[1][i][ss] != 0.0) {
  1156. sb = (i * 18) + ss;
  1157. i = -1;
  1158. }
  1159. else {
  1160. ss--;
  1161. if (ss < 0) {
  1162. i--;
  1163. ss = 17;
  1164. }
  1165. }
  1166. }
  1167. i = 0;
  1168. while (sfindex_long[header.SamplingFrequency][i] <= sb) {
  1169. i++;
  1170. }
  1171. scalefactorBand = i;
  1172. i = sfindex_long[header.SamplingFrequency][i];
  1173. for (; scalefactorBand < 8; scalefactorBand++) {
  1174. sb = sfindex_long[header.SamplingFrequency][scalefactorBand+1]
  1175. - sfindex_long[header.SamplingFrequency][scalefactorBand];
  1176. for (; sb > 0; sb--) {
  1177. decodedPos[i] = cast(short)scalefac[gr][1].long_window[scalefactorBand];
  1178. if (decodedPos[i] != 7) {
  1179. // IF (MPEG2) { ... }
  1180. // ELSE {
  1181. decodedRatio[i] = tan(cast(double)decodedPos[i] * (PI / 12.0));
  1182. // }
  1183. }
  1184. i++;
  1185. }
  1186. }
  1187. }
  1188. }
  1189. }
  1190. else {
  1191. i = 31;
  1192. ss = 17;
  1193. sb = 0;
  1194. while (i >= 0) {
  1195. if (quantizedData[1][i][ss] != 0.0) {
  1196. sb = (i * 18) + ss;
  1197. i = -1;
  1198. }
  1199. else {
  1200. ss--;
  1201. if (ss < 0) {
  1202. i--;
  1203. ss = 17;
  1204. }
  1205. }
  1206. }
  1207. i = 0;
  1208. while (sfindex_long[header.SamplingFrequency][i] <= sb) {
  1209. i++;
  1210. }
  1211. scalefactorBand = i;
  1212. i = sfindex_long[header.SamplingFrequency][i];
  1213. for (; scalefactorBand < 21; scalefactorBand++) {
  1214. sb = sfindex_long[header.SamplingFrequency][scalefactorBand+1]
  1215. - sfindex_long[header.SamplingFrequency][scalefactorBand];
  1216. for (; sb > 0; sb--) {
  1217. decodedPos[i] = cast(short)scalefac[gr][1].long_window[scalefactorBand];
  1218. if (decodedPos[i] != 7) {
  1219. // IF (MPEG2) { ... }
  1220. // ELSE {
  1221. decodedRatio[i] = tan(cast(double)decodedPos[i] * (PI / 12.0));
  1222. // }
  1223. }
  1224. i++;
  1225. }
  1226. }
  1227. scalefactorBand = sfindex_long[header.SamplingFrequency][20];
  1228. for (sb = 576 - sfindex_long[header.SamplingFrequency][21]; sb > 0; sb--) {
  1229. decodedPos[i] = decodedPos[scalefactorBand];
  1230. decodedRatio[i] = decodedRatio[scalefactorBand];
  1231. k[0][i] = k[0][scalefactorBand];
  1232. k[1][i] = k[1][scalefactorBand];
  1233. i++;
  1234. }
  1235. }
  1236. }
  1237. for (ch = 0; ch < 2; ch++) {
  1238. for (sb = 0; sb < SBLIMIT; sb++) {
  1239. for (ss = 0; ss < SSLIMIT; ss++) {
  1240. normalizedData[ch][sb][ss] = 0;
  1241. }
  1242. }
  1243. }
  1244. if (channels == 2) {
  1245. for (sb = 0; sb < SBLIMIT; sb++) {
  1246. for (ss = 0; ss < SSLIMIT; ss++) {
  1247. i = (sb * 18) + ss;
  1248. if (decodedPos[i] == 7) {
  1249. if (msStereo) {
  1250. normalizedData[0][sb][ss] = (quantizedData[0][sb][ss] + quantizedData[1][sb][ss])
  1251. / 1.41421356;
  1252. normalizedData[1][sb][ss] = (quantizedData[0][sb][ss] - quantizedData[1][sb][ss])
  1253. / 1.41421356;
  1254. }
  1255. else {
  1256. normalizedData[0][sb][ss] = quantizedData[0][sb][ss];
  1257. normalizedData[1][sb][ss] = quantizedData[1][sb][ss];
  1258. }
  1259. }
  1260. else if (intensityStereo) {
  1261. // IF (MPEG2) {
  1262. // normalizedData[0][sb][ss] = quantizedData[0][sb][ss] * k[0][i];
  1263. // normalizedData[1][sb][ss] = quantizedData[0][sb][ss] * k[1][i];
  1264. // }
  1265. // ELSE {
  1266. normalizedData[0][sb][ss] = quantizedData[0][sb][ss] * (decodedRatio[i] / (1 + decodedRatio[i]));
  1267. normalizedData[1][sb][ss] = quantizedData[0][sb][ss] * (1 / (1 + decodedRatio[i]));
  1268. // }
  1269. }
  1270. else {
  1271. // Error ...
  1272. }
  1273. }
  1274. }
  1275. }
  1276. else { // Mono
  1277. for (sb = 0; sb < SBLIMIT; sb++) {
  1278. for (ss = 0; ss < SSLIMIT; ss++) {
  1279. normalizedData[0][sb][ss] = quantizedData[0][sb][ss];
  1280. }
  1281. }
  1282. }
  1283. }
  1284. void reorder(uint gr, uint ch) {
  1285. int sfreq = header.SamplingFrequency;
  1286. for (uint sb; sb < SBLIMIT; sb++) {
  1287. for (uint ss; ss < SSLIMIT; ss++) {
  1288. reorderedData[sb][ss] = 0;
  1289. }
  1290. }
  1291. if ((blocksplit_flag[gr][ch] == 1) && (block_type[gr][ch] == 2)) {
  1292. if (switch_point[gr][ch] == 0) {
  1293. // Recoder the short blocks
  1294. uint scalefactorStart;
  1295. uint scalefactorLines;
  1296. for (uint scalefactorBand; scalefactorBand < 13; scalefactorBand++) {
  1297. scalefactorStart = sfindex_short[sfreq][scalefactorBand];
  1298. scalefactorLines = sfindex_short[sfreq][scalefactorBand + 1] - scalefactorStart;
  1299. for (uint window; window < 3; window++) {
  1300. for (uint freq; freq < scalefactorLines; freq++) {
  1301. uint srcLine = (scalefactorStart * 3) + (window * scalefactorLines) + freq;
  1302. uint destLine = (scalefactorStart * 3) + window + (freq * 3);
  1303. reorderedData[destLine / SSLIMIT][destLine % SSLIMIT] =
  1304. normalizedData[ch][srcLine / SSLIMIT][srcLine % SSLIMIT];
  1305. // printf("::%d %d %f\n", srcLine, destLine, reorderedData[destLine / SSLIMIT][destLine % SSLIMIT]);
  1306. }
  1307. }
  1308. }
  1309. }
  1310. else {
  1311. // We do not reorder the long blocks
  1312. for (uint sb; sb < 2; sb++) {
  1313. for (uint ss; ss < SSLIMIT; ss++) {
  1314. reorderedData[sb][ss] = normalizedData[ch][sb][ss];
  1315. }
  1316. }
  1317. // We reorder the short blocks
  1318. uint scalefactorStart;
  1319. uint scalefactorLines;
  1320. for (uint scalefactorBand = 3; scalefactorBand < 13; scalefactorBand++) {
  1321. scalefactorStart = sfindex_short[sfreq][scalefactorBand];
  1322. scalefactorLines = sfindex_short[sfreq][scalefactorBand + 1] - scalefactorStart;
  1323. for (uint window; window < 3; window++) {
  1324. for (uint freq; freq < scalefactorLines; freq++) {
  1325. uint srcLine = (scalefactorStart * 3) + (window * scalefactorLines) + freq;
  1326. uint destLine = (scalefactorStart * 3) + window + (freq * 3);
  1327. reorderedData[destLine / SSLIMIT][destLine % SSLIMIT] =
  1328. normalizedData[ch][srcLine / SSLIMIT][srcLine % SSLIMIT];
  1329. }
  1330. }
  1331. }
  1332. }
  1333. }
  1334. else {
  1335. // We do not reorder long blocks
  1336. for (uint sb; sb < SBLIMIT; sb++) {
  1337. for (uint ss; ss < SSLIMIT; ss++) {
  1338. reorderedData[sb][ss] = normalizedData[ch][sb][ss];
  1339. }
  1340. }
  1341. }
  1342. }
  1343. // Butterfly anti-alias
  1344. void antialias(uint gr, uint ch) {
  1345. uint subbandLimit = SBLIMIT - 1;
  1346. // Ci[i] = [-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037];
  1347. // cs[i] = 1.0 / (sqrt(1.0 + (Ci[i] * Ci[i])));
  1348. // ca[i] = ca[i] * Ci[i];
  1349. const double cs[8] = [
  1350. 0.85749292571254418689325777610964,
  1351. 0.88174199731770518177557399759066,
  1352. 0.94962864910273289204833276115398,
  1353. 0.98331459249179014599030200066392,
  1354. 0.99551781606758576429088040422867,
  1355. 0.99916055817814750452934664352117,
  1356. 0.99989919524444704626703489425565,
  1357. 0.99999315507028023572010150517204
  1358. ];
  1359. const double ca[8] = [
  1360. -0.5144957554275265121359546656654,
  1361. -0.47173196856497227224993208871065,
  1362. -0.31337745420390185437594981118049,
  1363. -0.18191319961098117700820587012266,
  1364. -0.09457419252642064760763363840166,
  1365. -0.040965582885304047685703212384361,
  1366. -0.014198568572471148056991895498421,
  1367. -0.0036999746737600368721643755691364
  1368. ];
  1369. // Init our working array with quantized data
  1370. for (uint sb; sb < SBLIMIT; sb++) {
  1371. for (uint ss; ss < SSLIMIT; ss++) {
  1372. hybridData[sb][ss] = reorderedData[sb][ss];
  1373. }
  1374. }
  1375. if ((blocksplit_flag[gr][ch] == 1) && (block_type[gr][ch] == 2) && (switch_point[gr][ch] == 0)) {
  1376. return;
  1377. }
  1378. if ((blocksplit_flag[gr][ch] == 1) && (block_type[gr][ch] == 2) && (switch_point[gr][ch] == 1)) {
  1379. subbandLimit = 1;
  1380. }
  1381. // 8 butterflies for each pair of subbands
  1382. for (uint sb; sb < subbandLimit; sb++) {
  1383. for (uint ss; ss < 8; ss++) {
  1384. double bu = reorderedData[sb][17 - ss];
  1385. double bd = reorderedData[sb + 1][ss];
  1386. hybridData[sb][17 - ss] = (bu * cs[ss]) - (bd * ca[ss]);
  1387. hybridData[sb + 1][ss] = (bd * cs[ss]) + (bu * ca[ss]);
  1388. }
  1389. }
  1390. }
  1391. // zerodouble initializes to 0.0 instead of nil
  1392. zerodouble[SSLIMIT][SBLIMIT][2] previousBlock;
  1393. void hybridSynthesis(uint gr, uint ch, uint sb) {
  1394. int blockType = block_type[gr][ch];
  1395. if ((blocksplit_flag[gr][ch] == 1) && (switch_point[gr][ch] == 1) && (sb < 2)) {
  1396. blockType = 0;
  1397. }
  1398. double[36] output = inverseMDCT(hybridData[sb], blockType);
  1399. // Overlapping and Adding with Previous Block:
  1400. // The last half gets reserved for the next block, and used in this block
  1401. for (uint ss; ss < SSLIMIT; ss++) {
  1402. polysynthData[ch][sb][ss] = output[ss] + previousBlock[ch][sb][ss];
  1403. previousBlock[ch][sb][ss] = cast(zerodouble)output[ss+18];
  1404. }
  1405. }
  1406. double[] inverseMDCT(double[18] working, int blockType) {
  1407. double[] ret = new double[36];
  1408. // The Constant Parts of the Windowing equations
  1409. const double[36][4] win = [
  1410. // Block Type 0
  1411. // win[i] = sin( (PI / 36) * ( i + 0.5 ) ) ; i = 0 to 35
  1412. [
  1413. 0.043619387365336000084, 0.130526192220051573400, 0.216439613938102876070,
  1414. 0.300705799504273119100, 0.382683432365089781770, 0.461748613235033911190,
  1415. 0.537299608346823887030, 0.608761429008720655880, 0.675590207615660132130,
  1416. 0.737277336810123973260, 0.793353340291235165080, 0.843391445812885720550,
  1417. 0.887010833178221602670, 0.923879532511286738480, 0.953716950748226821580,
  1418. 0.976296007119933362260, 0.991444861373810382150, 0.999048221581857798230,
  1419. 0.999048221581857798230, 0.991444861373810382150, 0.976296007119933362260,
  1420. 0.953716950748226932610, 0.923879532511286738480, 0.887010833178221824720,
  1421. 0.843391445812885831570, 0.793353340291235165080, 0.737277336810124084280,
  1422. 0.675590207615660354170, 0.608761429008720877930, 0.537299608346824109080,
  1423. 0.461748613235033911190, 0.382683432365089892800, 0.300705799504273341140,
  1424. 0.216439613938103181380, 0.130526192220051573400, 0.043619387365336069473
  1425. ],
  1426. // Block Type 1
  1427. // win[i] = sin( (PI / 36) * ( i + 0.5 ) ) ; i = 0 to 17
  1428. // win[i] = 1 ; i = 18 to 23
  1429. // win[i] = sin( (PI / 12) * ( i - 18 + 0.5 ) ) ; i = 24 to 29
  1430. // win[i] = 0 ; i = 30 to 35
  1431. [
  1432. 0.043619387365336000084, 0.130526192220051573400, 0.216439613938102876070,
  1433. 0.300705799504273119100, 0.382683432365089781770, 0.461748613235033911190,
  1434. 0.537299608346823887030, 0.608761429008720655880, 0.675590207615660132130,
  1435. 0.737277336810123973260, 0.793353340291235165080, 0.843391445812885720550,
  1436. 0.887010833178221602670, 0.923879532511286738480, 0.953716950748226821580,
  1437. 0.976296007119933362260, 0.991444861373810382150, 0.999048221581857798230,
  1438. 1.000000000000000000000, 1.000000000000000000000, 1.000000000000000000000,
  1439. 1.000000000000000000000, 1.000000000000000000000, 1.000000000000000000000,
  1440. 0.991444861373810382150, 0.923879532511286738480, 0.793353340291235165080,
  1441. 0.608761429008720433840, 0.382683432365089448710, 0.130526192220051129310,
  1442. 0.000000000000000000000, 0.000000000000000000000, 0.000000000000000000000,
  1443. 0.000000000000000000000, 0.000000000000000000000, 0.000000000000000000000
  1444. ],
  1445. // Block Type 2
  1446. // win[i] = sin( (PI / 12.0) * (i + 0.5) ) ; i = 0 to 11
  1447. // win[i] = 0.0 ; i = 12 to 35
  1448. [
  1449. 0.130526192220051601150, 0.382683432365089781770, 0.608761429008720766900,
  1450. 0.793353340291235165080, 0.923879532511286849500, 0.991444861373810382150,
  1451. 0.991444861373810382150, 0.923879532511286738480, 0.793353340291235165080,
  1452. 0.608761429008720433840, 0.382683432365089448710, 0.130526192220051129310,
  1453. 0.000000000000000000000, 0.000000000000000000000, 0.000000000000000000000,
  1454. 0.000000000000000000000, 0.000000000000000000000, 0.000000000000000000000,
  1455. 0.000000000000000000000, 0.000000000000000000000, 0.000000000000000000000,
  1456. 0.000000000000000000000, 0.000000000000000000000, 0.000000000000000000000,
  1457. 0.000000000000000000000, 0.000000000000000000000, 0.000000000000000000000,
  1458. 0.000000000000000000000, 0.000000000000000000000, 0.000000000000000000000,
  1459. 0.000000000000000000000, 0.000000000000000000000, 0.000000000000000000000,
  1460. 0.000000000000000000000, 0.000000000000000000000, 0.000000000000000000000
  1461. ],
  1462. // Block Type 3
  1463. // win[i] = 0 ; i = 0 to 5
  1464. // win[i] = sin( (PI / 12) * ( i - 6 + 0.5 ) ) ; i = 6 to 11
  1465. // win[i] = 1 ; i = 12 to 17
  1466. // win[i] = sin( (PI / 36) * ( i + 0.5 ) ) ; i = 18 to 35
  1467. [
  1468. 0.000000000000000000000, 0.000000000000000000000, 0.000000000000000000000,
  1469. 0.000000000000000000000, 0.000000000000000000000, 0.000000000000000000000,
  1470. 0.130526192220051601150, 0.382683432365089781770, 0.608761429008720766900,
  1471. 0.793353340291235165080, 0.923879532511286849500, 0.991444861373810382150,
  1472. 1.000000000000000000000, 1.000000000000000000000, 1.000000000000000000000,
  1473. 1.000000000000000000000, 1.000000000000000000000, 1.000000000000000000000,
  1474. 0.999048221581857798230, 0.991444861373810382150, 0.976296007119933362260,
  1475. 0.953716950748226932610, 0.923879532511286738480, 0.887010833178221824720,
  1476. 0.843391445812885831570, 0.793353340291235165080, 0.737277336810124084280,
  1477. 0.675590207615660354170, 0.608761429008720877930, 0.537299608346824109080,
  1478. 0.461748613235033911190, 0.382683432365089892800, 0.300705799504273341140,
  1479. 0.216439613938103181380, 0.130526192220051573400, 0.043619387365336069473
  1480. ]
  1481. ];
  1482. double[4*36] cosLookup = [
  1483. 1.000000000000000000000, 0.999048221581857798230, 0.996194698091745545190, 0.991444861373810382150,
  1484. 0.984807753012208020310, 0.976296007119933362260, 0.965925826289068312210, 0.953716950748226932610,
  1485. 0.939692620785908427900, 0.923879532511286738480, 0.906307787036649936670, 0.887010833178221713700,
  1486. 0.866025403784438707610, 0.843391445812885720550, 0.819152044288991798550, 0.793353340291235165080,
  1487. 0.766044443118978013450, 0.737277336810124084280, 0.707106781186547572730, 0.675590207615660354170,
  1488. 0.642787609686539362910, 0.608761429008720655880, 0.573576436351046159420, 0.537299608346823887030,
  1489. 0.500000000000000111020, 0.461748613235034077720, 0.422618261740699441280, 0.382683432365089837290,
  1490. 0.342020143325668823930, 0.300705799504273285630, 0.258819045102520739470, 0.216439613938102903830,
  1491. 0.173648177666930414450, 0.130526192220051712170, 0.087155742747658360158, 0.043619387365336007023,
  1492. 0.000000000000000061230, -0.043619387365335889061, -0.087155742747658013214, -0.130526192220051601150,
  1493. -0.173648177666930303430, -0.216439613938102792810, -0.258819045102520628450, -0.300705799504272952570,
  1494. -0.342020143325668712900, -0.382683432365089726260, -0.422618261740699330260, -0.461748613235033744660,
  1495. -0.499999999999999777950, -0.537299608346823553970, -0.573576436351045826350, -0.608761429008720655880,
  1496. -0.642787609686539362910, -0.675590207615660243150, -0.707106781186547461710, -0.737277336810123973260,
  1497. -0.766044443118977902420, -0.793353340291235054060, -0.819152044288991576510, -0.843391445812885498510,
  1498. -0.866025403784438707610, -0.887010833178221713700, -0.906307787036649936670, -0.923879532511286738480,
  1499. -0.939692620785908316880, -0.953716950748226821580, -0.965925826289068201190, -0.976296007119933251230,
  1500. -0.984807753012208020310, -0.991444861373810382150, -0.996194698091745545190, -0.999048221581857798230,
  1501. -1.000000000000000000000, -0.999048221581857798230, -0.996194698091745545190, -0.991444861373810493170,
  1502. -0.984807753012208131330, -0.976296007119933473280, -0.965925826289068312210, -0.953716950748226932610,
  1503. -0.939692620785908427900, -0.923879532511286849500, -0.906307787036650047690, -0.887010833178221824720,
  1504. -0.866025403784438818630, -0.843391445812885831570, -0.819152044288992020600, -0.793353340291235165080,
  1505. -0.766044443118978013450, -0.737277336810124084280, -0.707106781186547683750, -0.675590207615660354170,
  1506. -0.642787609686539473940, -0.608761429008720877930, -0.573576436351046381460, -0.537299608346824220100,
  1507. -0.500000000000000444080, -0.461748613235034410790, -0.422618261740699940880, -0.382683432365090336890,
  1508. -0.342020143325669379040, -0.300705799504272952570, -0.258819045102520628450, -0.216439613938102820560,
  1509. -0.173648177666930331180, -0.130526192220051628910, -0.087155742747658249136, -0.043619387365336131923,
  1510. -0.000000000000000183690, 0.043619387365335764161, 0.087155742747657888314, 0.130526192220051268080,
  1511. 0.173648177666929970360, 0.216439613938102459740, 0.258819045102520295380, 0.300705799504272619500,
  1512. 0.342020143325668157790, 0.382683432365089171150, 0.422618261740698830660, 0.461748613235034077720,
  1513. 0.500000000000000111020, 0.537299608346823887030, 0.573576436351046048400, 0.608761429008720544860,
  1514. 0.642787609686539251890, 0.675590207615660132130, 0.707106781186547350690, 0.737277336810123862240,
  1515. 0.766044443118977791400, 0.793353340291234943030, 0.819152044288991576510, 0.843391445812885498510,
  1516. 0.866025403784438374540, 0.887010833178221491650, 0.906307787036649714620, 0.923879532511286516430,
  1517. 0.939692620785908094830, 0.953716950748226710560, 0.965925826289068312210, 0.976296007119933362260,
  1518. 0.984807753012208020310, 0.991444861373810382150, 0.996194698091745545190, 0.999048221581857798230,
  1519. ];
  1520. // Zero to initialize
  1521. ret[0..$] = 0.0;
  1522. if (blockType == 2) {
  1523. uint N = 12;
  1524. for (uint i; i < 3; i++) {
  1525. double[12] tmp;
  1526. for (uint p; p < N; p++) {
  1527. double sum = 0.0;
  1528. for (uint m; m < (N / 2); m++) {
  1529. sum += working[i + (3 * m)] * cos((PI / cast(double)(2 * N)) * cast(double)((2 * p) + 1 + (N / 2)) * ((2 * m) + 1));
  1530. // if (sum > 0.0) printf("sa:%f\n", sum);
  1531. }
  1532. tmp[p] = sum * win[2][p];
  1533. }
  1534. for (uint p; p < N; p++) {
  1535. ret[(6 * i) + p + 6] += tmp[p];
  1536. }
  1537. }
  1538. }
  1539. else {
  1540. uint N = 36;
  1541. for (uint p; p < 36; p++) {
  1542. double sum = 0.0;
  1543. for (uint m; m < 18; m++) {
  1544. sum += working[m] * cosLookup[(((2 * p) + 1 + 18) * ((2 * m) + 1)) % (4 * 36)];
  1545. }
  1546. ret[p] = sum * win[blockType][p];
  1547. }
  1548. }
  1549. return ret;
  1550. }
  1551. void polyphaseSynthesis(uint gr, uint ss, ref Wavelet toBuffer) {
  1552. double sum;
  1553. uint i;
  1554. uint k;
  1555. uint j;
  1556. long foo;
  1557. double* bufOffsetPtr;
  1558. double* bufOffsetPtr2;
  1559. if (channels == 1) {
  1560. uint channel = 0;
  1561. bufOffset[channel] = (bufOffset[channel] - 64) & 0x3ff;
  1562. bufOffsetPtr = cast(double*)&BB[channel][bufOffset[channel]];
  1563. for (i=0; i<64; i++) {
  1564. sum = 0;
  1565. for (k=0; k<32; k++) {
  1566. sum += polysynthData[channel][k][ss] * nCoefficients[i][k];
  1567. }
  1568. bufOffsetPtr[i] = sum;
  1569. }
  1570. for (j=0; j<32; j++) {
  1571. sum = 0;
  1572. for (i=0; i<16; i++) {
  1573. k = j + (i << 5);
  1574. sum += windowCoefficients[k] * BB[channel][((k + (((i + 1) >> 1) << 6)) + bufOffset[channel]) & 0x3ff];
  1575. }
  1576. if(sum > 0) {
  1577. foo = cast(long)(sum * cast(double)32768 + cast(double)0.5);
  1578. }
  1579. else {
  1580. foo = cast(long)(sum * cast(double)32768 - cast(double)0.5);
  1581. }
  1582. if (foo >= cast(long)32768) {
  1583. toBuffer.write(cast(short)(32768-1));
  1584. //++clip;
  1585. }
  1586. else if (foo < cast(long)-32768) {
  1587. toBuffer.write(cast(short)(-32768));
  1588. //++clip;
  1589. }
  1590. else {
  1591. toBuffer.write(cast(short)foo);
  1592. }
  1593. //printf("%d\n", foo);
  1594. }
  1595. }
  1596. else {
  1597. // INTERLEAVE CHANNELS!
  1598. bufOffset[0] = (bufOffset[0] - 64) & 0x3ff;
  1599. bufOffsetPtr = cast(double*)&BB[0][bufOffset[0]];
  1600. bufOffset[1] = (bufOffset[1] - 64) & 0x3ff;
  1601. bufOffsetPtr2 = cast(double*)&BB[1][bufOffset[1]];
  1602. double sum2;
  1603. for (i=0; i<64; i++) {
  1604. sum = 0;
  1605. sum2 = 0;
  1606. for (k=0; k<32; k++) {
  1607. sum += polysynthData[0][k][ss] * nCoefficients[i][k];
  1608. sum2 += polysynthData[1][k][ss] * nCoefficients[i][k];
  1609. }
  1610. bufOffsetPtr[i] = sum;
  1611. bufOffsetPtr2[i] = sum2;
  1612. }
  1613. long[32] ch1;
  1614. long[32] ch2;
  1615. for (j=0; j<32; j++) {
  1616. sum = 0;
  1617. sum2 = 0;
  1618. for (i=0; i<16; i++) {
  1619. k = j + (i << 5);
  1620. sum += windowCoefficients[k] * BB[0][( (k + ( ((i+1)>>1) << 6) ) + bufOffset[0]) & 0x3ff];
  1621. sum2 += windowCoefficients[k] * BB[1][( (k + ( ((i+1)>>1) << 6) ) + bufOffset[1]) & 0x3ff];
  1622. }
  1623. if(sum > 0) {
  1624. foo = cast(long)(sum * cast(double)32768 + cast(double)0.5);
  1625. }
  1626. else {
  1627. foo = cast(long)(sum * cast(double)32768 - cast(double)0.5);
  1628. }
  1629. if (foo >= cast(long)32768) {
  1630. toBuffer.write(cast(short)(32768-1));
  1631. //++clip;
  1632. ch1[j] = 32768;
  1633. }
  1634. else if (foo < cast(long)-32768) {
  1635. toBuffer.write(cast(short)(-32768));
  1636. //++clip;
  1637. ch1[j] = -32768;
  1638. }
  1639. else {
  1640. toBuffer.write(cast(short)foo);
  1641. ch1[j] = foo;
  1642. }
  1643. if(sum2 > 0) {
  1644. foo = cast(long)(sum2 * cast(double)32768 + cast(double)0.5);
  1645. }
  1646. else {
  1647. foo = cast(long)(sum2 * cast(double)32768 - cast(double)0.5);
  1648. }
  1649. if (foo >= cast(long)32768) {
  1650. toBuffer.write(cast(short)(32768-1));
  1651. //++clip;
  1652. ch2[j] = 32768;
  1653. }
  1654. else if (foo < cast(long)-32768) {
  1655. toBuffer.write(cast(short)(-32768));
  1656. //++clip;
  1657. ch2[j] = -32768;
  1658. }
  1659. else {
  1660. toBuffer.write(cast(short)foo);
  1661. ch2[j] = foo;
  1662. }
  1663. // printf("%d\n", ch1[j]);
  1664. }
  1665. for (j=0; j < 32; j++) {
  1666. // printf("%d\n", ch2[j]);
  1667. }
  1668. }
  1669. }
  1670. uint readBits(uint bits) {
  1671. // read a byte, read bits, whatever necessary to get the value
  1672. //writeln("reading, # bits:", bits, " curbyte:", *curByte);
  1673. uint curvalue;
  1674. uint value = 0;
  1675. uint mask;
  1676. uint maskbits;
  1677. int shift;
  1678. if (curByte >= audioRefLength) {
  1679. // We have consumed everything in our buffer
  1680. return 5;
  1681. }
  1682. for (;;) {
  1683. if (bits == 0) { return value; }
  1684. if (bits > 8) {
  1685. maskbits = 8;
  1686. }
  1687. else {
  1688. maskbits = bits;
  1689. }
  1690. //Console.putln("curpos:", curPos, " for bits:", bits, " maskbits:", maskbits);
  1691. curvalue = (audioRef[curByte] & (byteMasks[maskbits][curPos]));
  1692. shift = ((8-cast(int)curPos) - cast(int)bits);
  1693. if (shift > 0) {
  1694. curvalue >>= shift;
  1695. }
  1696. else if (shift < 0) {
  1697. curvalue <<= -shift;
  1698. }
  1699. //writeln("has curvalue:", curvalue);
  1700. value |= curvalue;
  1701. //writeln("has value:", value);
  1702. curPos += maskbits;
  1703. if (curPos >= 8) {
  1704. bits -= (8 - curPos + maskbits);
  1705. curPos = 0;
  1706. curByte++;
  1707. if (curByte >= audioRefLength) {
  1708. // We have consumed everything in our buffer
  1709. return value;
  1710. }
  1711. }
  1712. else {
  1713. break;
  1714. }
  1715. }
  1716. return value;
  1717. }
  1718. // header for the frame
  1719. uint mpeg_header;
  1720. // bit building
  1721. uint curByte;
  1722. uint curPos;
  1723. uint bufferLength;
  1724. uint audioHeaderLength;
  1725. // header information for the frame
  1726. MP3HeaderInformation header;
  1727. ID3HeaderInformation id3;
  1728. // the number of channels present
  1729. uint channels;
  1730. ushort crc;
  1731. // the number of samples left to decode
  1732. int samplesLeft;
  1733. // the buffer size and information
  1734. uint bufferSize;
  1735. Time bufferTime;
  1736. AudioFormat wf;
  1737. ubyte[] audioRef;
  1738. uint audioRefLength;
  1739. ubyte[] audioData;
  1740. ubyte[32] audioHeader;
  1741. uint frame_start;
  1742. uint frame_end;
  1743. uint syncAmount;
  1744. uint id3length;
  1745. // the main data end pointer
  1746. uint main_data_end;
  1747. uint main_data_begin;
  1748. // the current pointer (of last frame)
  1749. uint cur_data_ptr;
  1750. // cb_limit -- Number of scalefactor bands for long blocks
  1751. // (block_type != 2). This is a constant, 21, for Layer III
  1752. // in all modes and at all sampling frequencies.
  1753. const uint cb_limit = 21;
  1754. // cb_limit_short -- Number of scalefactor bands for long
  1755. // blocks (block_type == 2). This is a constant, 12, for
  1756. // Layer III in all modes and at all sampling frequencies.
  1757. const uint cb_limit_short = 12;
  1758. // scfsi[scfsi_band] -- In Layer III, the scalefactor
  1759. // selection information works similarly to Layers I and
  1760. // II. The main difference is the use of variable
  1761. // scfsi_band to apply scfsi to groups of scalefactors
  1762. // instead of single scalefactors. scfsi controls the
  1763. // use of scalefactors to the granules.
  1764. uint[2][cb_limit] scfsi;
  1765. // part2_3_length[gr] -- This value contains the number of
  1766. // main_data bits used for scalefactors and Huffman code
  1767. // data. Because the length of the side information is
  1768. // always the same, this value can be used to calculate
  1769. // the beginning of the main information for each granule
  1770. // and the position of ancillary information (if used).
  1771. uint[2][2] part2_3_length;
  1772. // part2_length -- This value contains the number of
  1773. // main_data bits used for scalefactors and Huffman code
  1774. // data. Because the length of the side information is
  1775. // always the same, this value can be used to calculate
  1776. // the beginning of the main information for each granule
  1777. // and the position of ancillary information (if used).
  1778. uint part2_length;
  1779. // big_values[gr] -- The spectral values of each granule
  1780. // are coded with different Huffman code tables. The full
  1781. // frequency range from zero to the Nyquist frequency is
  1782. // divided into several regions, which then are coded using
  1783. // different table. Partitioning is done according to the
  1784. // maximum quantized values. This is done with the
  1785. // assumption the values at higher frequencies are expected
  1786. // to have lower amplitudes or don't need to be coded at all.
  1787. // Starting at high frequencies, the pairs of quantized values
  1788. // with absolute value not exceeding 1 (i.e. only 3 possible
  1789. // quantization levels) are counted. This number is named
  1790. // "count1". Again, an even number of values remains. Finally,
  1791. // the number of pairs of values in the region of the spectrum
  1792. // which extends down to zero is named "big_values". The
  1793. // maximum absolute value in this range is constrained to 8191.
  1794. // The figure shows the partitioning:
  1795. // xxxxxxxxxxxxx------------------00000000000000000000000000000
  1796. // | | | |
  1797. // 1 bigvalues*2 bigvalues*2+count1*4 iblen
  1798. //
  1799. // The values 000 are all zero
  1800. // The values --- are -1, 0, or +1. Their number is a multiple of 4
  1801. // The values xxx are not bound
  1802. // iblen = 576
  1803. uint[2][2] big_values;
  1804. // global_gain[gr] -- The quantizer step size information is
  1805. // transmitted in the side information variable global_gain.
  1806. // It is logarithmically quantized. For the application of
  1807. // global_gain, refer to the formula in 2.4.3.4 (ISO 11172-3)
  1808. uint[2][2] global_gain;
  1809. // scalefac_compress[gr] -- selects the number of bits used
  1810. // for the transmission of the scalefactors according to the
  1811. // following table:
  1812. // if block_type is 0, 1, or 3:
  1813. // slen1: length of scalefactors for scalefactor bands 0 to 10
  1814. // slen2: length of scalefactors for scalefactor bands 11 to 20
  1815. // if block_type is 2 and switch_point is 0:
  1816. // slen1: length of scalefactors for scalefactor bands 0 to 5
  1817. // slen2: length of scalefactors for scalefactor bands 6 to 11
  1818. // if block_type is 2 and switch_point is 1:
  1819. // slen1: length of scalefactors for scalefactor bands 0 to 7
  1820. // (long window scalefactor band) and 4 to 5 (short window
  1821. // scalefactor band)
  1822. // slen2: length of scalefactors for scalefactor bands 6 to 11
  1823. // scalefactor_compress slen1 slen2
  1824. // 0 0 0
  1825. // 1 0 1
  1826. // 2 0 2
  1827. // 3 0 3
  1828. // 4 3 0
  1829. // 5 1 1
  1830. // 6 1 2
  1831. // 7 1 3
  1832. // 8 2 1
  1833. // 9 2 2
  1834. // 10 2 3
  1835. // 11 3 1
  1836. // 12 3 2
  1837. // 13 3 3
  1838. // 14 4 2
  1839. // 15 4 3
  1840. uint[2][2] scalefac_compress;
  1841. uint[2][2] slen1;
  1842. uint[2][2] slen2;
  1843. const int[16] slen1_interpret = [0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4];
  1844. const int[16] slen2_interpret = [0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3];
  1845. // blocksplit_flag[gr] -- signals that the block uses an other
  1846. // than normal (type 0) window. If blocksplit_flag is set,
  1847. // several other variables are set by default:
  1848. // region_address1 = 8 (in case of block_type == 1 or 3)
  1849. // region_address1 = 9 (in case of block_type == 2)
  1850. // region_address2 = 0 and the length of region 2 is zero
  1851. // If it is not set, the value of block_type is zero
  1852. uint[2][2] blocksplit_flag;
  1853. // block_type[gr] -- indicates the window type for the actual
  1854. // granule (see the description of filterbank)
  1855. // type 0 - reserved
  1856. // type 1 - start block
  1857. // type 2 - 3 short windows
  1858. // type 3 - end block
  1859. uint[2][2] block_type;
  1860. // switch_point[gr] -- signals the split point of short/long
  1861. // transforms. The following table shows the number of the
  1862. // scalefactor band above which window switching (i.e.
  1863. // block_type different from 0) is used.
  1864. // switch_point switch_point_l switch_point_s
  1865. // (num of sb) (num of sb)
  1866. // --------------------------------------------
  1867. // 0 0 0
  1868. // 1 8 3
  1869. uint[2][2] switch_point;
  1870. // switch_point_l -- Number of the scalefactor band (long
  1871. // block scalefactor band) from which point on window
  1872. // switching is used.
  1873. uint[2][2] switch_point_l;
  1874. // switch_point_s -- Number of the scalefactor band (short
  1875. // block scalefactor band) from which point on window
  1876. // switching is used.
  1877. uint[2][2] switch_point_s;
  1878. // table_select[region][gr] -- different Huffman code tables
  1879. // are used depending on the maximum quantized value and the
  1880. // local statistics of the signal. There are a total of 32
  1881. // possible tables given in 3-Annex-B Table 3-B.7 (ISO 11172-3)
  1882. uint[2][2][3] table_select;
  1883. // subblock_gain[window][gr] -- indicates the gain offset
  1884. // (quantization: factor 4) from the global gain for one
  1885. // subblock. Used only with block type 2 (short windows).
  1886. // The values of the subblock have to be divided by 4.
  1887. uint[2][2][3] subblock_gain;
  1888. // region_address1[gr] -- A further partitioning of the
  1889. // spectrum is used to enhance the performance of the Huffman
  1890. // coder. It is a subdivision of the region which is described
  1891. // by big_values. The purpose of this subdivision is to get
  1892. // better error robustness and better coding efficiency. Three
  1893. // regions are used. Each region is coded using a different
  1894. // Huffman code table depending on the maximum quantized value
  1895. // and the local statistics.
  1896. // The values region_address[1,2] are used to point to the
  1897. // boundaries of the regions. The region boundaries are
  1898. // aligned with the partitioning of the spectrum into critical
  1899. // bands.
  1900. // In the case of block_type == 2 (short blocks) the scalefactor
  1901. // bands representing the different time slots are counted
  1902. // separately. If switch_point == 0, the total amount of scalefactor
  1903. // bands for the granule in this case is 12*3=36. If block_type == 2
  1904. // and switch_point == 1, the amount of scalefactor bands is
  1905. // 8+9*3=35. region_address1 counts the number of scalefactor bands
  1906. // until the upper edge of the first region:
  1907. // region_address1 upper_edge of region is upper edge
  1908. // of scalefactor band number:
  1909. // 0 0 (no first region)
  1910. // 1 1
  1911. // 2 2
  1912. // ... ...
  1913. // 15 15
  1914. uint[2][2] region_address1;
  1915. // region_address2[gr] -- counts the number of scalefactor bands
  1916. // which are partially or totally in region 3. Again, if
  1917. // block_type == 2, then the scalefactor bands representing
  1918. // different time slots are counted separately.
  1919. uint[2][2] region_address2;
  1920. // preflag[gr] -- This is a shortcut for additional high
  1921. // frequency amplification of the quantized values. If preflag
  1922. // is set, the values of a table are added to the scalefactors
  1923. // (see 3-Annex B, Table 3-B.6 ISO 11172-3). This is equivalent
  1924. // to multiplication of the requantized scalefactors with table
  1925. // values. preflag is never used if block_type == 2 (short blocks)
  1926. uint[2][2] preflag;
  1927. // scalefac_scale[gr] -- The scalefactors are logarithmically
  1928. // quantized with a step size of 2 or sqrt(2) depending on
  1929. // the value of scalefac_scale:
  1930. // scalefac_scale = 0 ... stepsize = sqrt(2)
  1931. // scalefac_scale = 1 ... stepsize = 2
  1932. uint[2][2] scalefac_scale;
  1933. // count1table_select[gr] -- This flag selects one of two
  1934. // possible Huffman code tables for the region of
  1935. // quadruples of quantized values with magnitude not
  1936. // exceeding 1. (ref to ISO 11172-3)
  1937. // count1table_select = 0 .. Table A of 3-Annex B.7
  1938. // count1table_select = 1 .. Table B of 3-Annex B.7
  1939. uint[2][2] count1table_select;
  1940. // scalefac[cb][gr] -- The scalefactors are used to color
  1941. // the quantization noise. If the quantization noise is
  1942. // colored with the right shape, it is masked completely.
  1943. // Unlike Layers I and II, the Layer III scalefactors say
  1944. // nothing about the local maximum of the quantized signal.
  1945. // In Layer III, the blocks stretch over several frequency
  1946. // lines. These blocks are called scalefactor bands and are
  1947. // selected to resemble critical bands as closely as possible.
  1948. // The scalefac_compress table shows that scalefactors 0-10
  1949. // have a range of 0 to 15 (maximum length 4 bits) and the
  1950. // scalefactors 11-21 have a range of 0 to 7 (3 bits).
  1951. // If intensity_stereo is enabled (modebit_extension) the
  1952. // scalefactors of the "zero_part" of the difference (right)
  1953. // channel are used as intensity_stereo positions
  1954. // The subdivision of the spectrum into scalefactor bands is
  1955. // fixed for every block length and sampling frequency and
  1956. // stored in tables in the coder and decoder. (3-Annex 3-B.8)
  1957. // The scalefactors are logarithmically quantized. The
  1958. // quantization step is set with scalefac_scale.
  1959. // scalefac[cb][window][gr] -- same as scalefac[cb][gr] but for
  1960. // different windows when block_type == 2.
  1961. struct ScaleFactor {
  1962. uint[3][13] short_window;
  1963. uint[23] long_window;
  1964. }
  1965. ScaleFactor[2][2] scalefac;
  1966. }