PageRenderTime 488ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/decoders/audio/mp3.d

http://github.com/wilkie/djehuty
D | 2440 lines | 342 code | 117 blank | 1981 comment | 62 complexity | fa55c9c98c9834a83ed3bbd9f7d0c65d MD5 | raw file

Large files files are truncated, but you can click here to view the full 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.60876142900

Large files files are truncated, but you can click here to view the full file