An audio player for macOS 10.8 and newer. https://kode54.net/cog
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1686 lines
69KB

  1. /**
  2. * clHCA DECODER
  3. *
  4. * - Original decompilation and C++ decoder by nyaga
  5. * https://github.com/Nyagamon/HCADecoder
  6. * - Ported to C by kode54
  7. * https://gist.github.com/kode54/ce2bf799b445002e125f06ed833903c0
  8. * - Cleaned up by bnnm using Thealexbarney's VGAudio decoder as reference
  9. * https://github.com/Thealexbarney/VGAudio
  10. */
  11. /* TODO:
  12. * - improve portability on types and float casts, sizeof(int) isn't necessarily sizeof(float)
  13. * - check "packed_noise_level" vs VGAudio (CriHcaPacking.UnpackFrameHeader), weird behaviour
  14. * - check "delta scalefactors" vs VGAudio (CriHcaPacking.DeltaDecode), may be setting wrong values on bad data
  15. * - check "read intensity" vs VGAudio (CriHcaPacking.ReadIntensity), skips intensities if first is 15
  16. * - simplify DCT4 code
  17. * - add extra validations: encoder_delay/padding < sample_count, bands/totals (max: 128?), track count==1, etc
  18. * - calling clHCA_clear multiple times will not deallocate "comment" correctly
  19. */
  20. //--------------------------------------------------
  21. // Includes
  22. //--------------------------------------------------
  23. #include "clHCA.h"
  24. #include <stddef.h>
  25. #include <stdlib.h>
  26. #include <memory.h>
  27. #define HCA_MASK 0x7F7F7F7F /* chunk obfuscation when the HCA is encrypted with key */
  28. #define HCA_SUBFRAMES_PER_FRAME 8
  29. #define HCA_SAMPLES_PER_SUBFRAME 128
  30. #define HCA_SAMPLES_PER_FRAME (HCA_SUBFRAMES_PER_FRAME*HCA_SAMPLES_PER_SUBFRAME)
  31. #define HCA_MDCT_BITS 7 /* (1<<7) = 128 */
  32. #define HCA_MAX_CHANNELS 16 /* internal max? in practice only 8 can be encoded */
  33. #define HCA_ERROR_PARAMS -1
  34. #define HCA_ERROR_HEADER -2
  35. #define HCA_ERROR_CHECKSUM -3
  36. #define HCA_ERROR_SYNC -4
  37. #define HCA_ERROR_UNPACK -5
  38. #define HCA_ERROR_BITREADER -6
  39. //--------------------------------------------------
  40. // Decoder config/state
  41. //--------------------------------------------------
  42. typedef enum { DISCRETE = 0, STEREO_PRIMARY = 1, STEREO_SECONDARY = 2 } channel_type_t;
  43. typedef struct stChannel {
  44. /* HCA channel config */
  45. int type; /* discrete / stereo-primary / stereo-secondary */
  46. unsigned int coded_scalefactor_count; /* scalefactors used (depending on channel type) */
  47. unsigned char *hfr_scales; /* high frequency scales, pointing to higher scalefactors (simplification) */
  48. /* subframe state */
  49. unsigned char intensity[HCA_SUBFRAMES_PER_FRAME]; /* intensity indexes (value max: 15 / 4b) */
  50. unsigned char scalefactors[HCA_SAMPLES_PER_SUBFRAME]; /* scale indexes (value max: 64 / 6b)*/
  51. unsigned char resolution[HCA_SAMPLES_PER_SUBFRAME]; /* resolution indexes (value max: 15 / 4b) */
  52. float gain[HCA_SAMPLES_PER_SUBFRAME]; /* gain to apply to quantized spectral data */
  53. float spectra[HCA_SAMPLES_PER_SUBFRAME]; /* resulting dequantized data */
  54. float temp[HCA_SAMPLES_PER_SUBFRAME]; /* temp for DCT-IV */
  55. float dct[HCA_SAMPLES_PER_SUBFRAME]; /* result of DCT-IV */
  56. float imdct_previous[HCA_SAMPLES_PER_SUBFRAME]; /* IMDCT */
  57. /* frame state */
  58. float wave[HCA_SUBFRAMES_PER_FRAME][HCA_SAMPLES_PER_SUBFRAME]; /* resulting samples */
  59. } stChannel;
  60. typedef struct clHCA {
  61. /* header config */
  62. unsigned int is_valid;
  63. /* hca chunk */
  64. unsigned int version;
  65. unsigned int header_size;
  66. /* fmt chunk */
  67. unsigned int channels;
  68. unsigned int sample_rate;
  69. unsigned int frame_count;
  70. unsigned int encoder_delay;
  71. unsigned int encoder_padding;
  72. /* comp/dec chunk */
  73. unsigned int frame_size;
  74. unsigned int min_resolution;
  75. unsigned int max_resolution;
  76. unsigned int track_count;
  77. unsigned int channel_config;
  78. unsigned int stereo_type;
  79. unsigned int total_band_count;
  80. unsigned int base_band_count;
  81. unsigned int stereo_band_count;
  82. unsigned int bands_per_hfr_group;
  83. unsigned int reserved1;
  84. unsigned int reserved2;
  85. /* vbr chunk */
  86. unsigned int vbr_max_frame_size;
  87. unsigned int vbr_noise_Level;
  88. /* ath chunk */
  89. unsigned int ath_type;
  90. /* loop chunk */
  91. unsigned int loop_start_frame;
  92. unsigned int loop_end_frame;
  93. unsigned int loop_start_delay;
  94. unsigned int loop_end_padding;
  95. unsigned int loop_flag;
  96. /* ciph chunk */
  97. unsigned int ciph_type;
  98. unsigned long long keycode;
  99. /* rva chunk */
  100. float rva_volume;
  101. /* comm chunk */
  102. unsigned int comment_len;
  103. char *comment;
  104. /* initial state */
  105. unsigned int hfr_group_count;
  106. unsigned char ath_curve[HCA_SAMPLES_PER_SUBFRAME];
  107. unsigned char cipher_table[256];
  108. /* variable state */
  109. stChannel channel[HCA_MAX_CHANNELS];
  110. } clHCA;
  111. typedef struct clData {
  112. const unsigned char *data;
  113. int size;
  114. int bit;
  115. } clData;
  116. //--------------------------------------------------
  117. // Checksum
  118. //--------------------------------------------------
  119. static const unsigned short crc16_lookup_table[256] = {
  120. 0x0000,0x8005,0x800F,0x000A,0x801B,0x001E,0x0014,0x8011,0x8033,0x0036,0x003C,0x8039,0x0028,0x802D,0x8027,0x0022,
  121. 0x8063,0x0066,0x006C,0x8069,0x0078,0x807D,0x8077,0x0072,0x0050,0x8055,0x805F,0x005A,0x804B,0x004E,0x0044,0x8041,
  122. 0x80C3,0x00C6,0x00CC,0x80C9,0x00D8,0x80DD,0x80D7,0x00D2,0x00F0,0x80F5,0x80FF,0x00FA,0x80EB,0x00EE,0x00E4,0x80E1,
  123. 0x00A0,0x80A5,0x80AF,0x00AA,0x80BB,0x00BE,0x00B4,0x80B1,0x8093,0x0096,0x009C,0x8099,0x0088,0x808D,0x8087,0x0082,
  124. 0x8183,0x0186,0x018C,0x8189,0x0198,0x819D,0x8197,0x0192,0x01B0,0x81B5,0x81BF,0x01BA,0x81AB,0x01AE,0x01A4,0x81A1,
  125. 0x01E0,0x81E5,0x81EF,0x01EA,0x81FB,0x01FE,0x01F4,0x81F1,0x81D3,0x01D6,0x01DC,0x81D9,0x01C8,0x81CD,0x81C7,0x01C2,
  126. 0x0140,0x8145,0x814F,0x014A,0x815B,0x015E,0x0154,0x8151,0x8173,0x0176,0x017C,0x8179,0x0168,0x816D,0x8167,0x0162,
  127. 0x8123,0x0126,0x012C,0x8129,0x0138,0x813D,0x8137,0x0132,0x0110,0x8115,0x811F,0x011A,0x810B,0x010E,0x0104,0x8101,
  128. 0x8303,0x0306,0x030C,0x8309,0x0318,0x831D,0x8317,0x0312,0x0330,0x8335,0x833F,0x033A,0x832B,0x032E,0x0324,0x8321,
  129. 0x0360,0x8365,0x836F,0x036A,0x837B,0x037E,0x0374,0x8371,0x8353,0x0356,0x035C,0x8359,0x0348,0x834D,0x8347,0x0342,
  130. 0x03C0,0x83C5,0x83CF,0x03CA,0x83DB,0x03DE,0x03D4,0x83D1,0x83F3,0x03F6,0x03FC,0x83F9,0x03E8,0x83ED,0x83E7,0x03E2,
  131. 0x83A3,0x03A6,0x03AC,0x83A9,0x03B8,0x83BD,0x83B7,0x03B2,0x0390,0x8395,0x839F,0x039A,0x838B,0x038E,0x0384,0x8381,
  132. 0x0280,0x8285,0x828F,0x028A,0x829B,0x029E,0x0294,0x8291,0x82B3,0x02B6,0x02BC,0x82B9,0x02A8,0x82AD,0x82A7,0x02A2,
  133. 0x82E3,0x02E6,0x02EC,0x82E9,0x02F8,0x82FD,0x82F7,0x02F2,0x02D0,0x82D5,0x82DF,0x02DA,0x82CB,0x02CE,0x02C4,0x82C1,
  134. 0x8243,0x0246,0x024C,0x8249,0x0258,0x825D,0x8257,0x0252,0x0270,0x8275,0x827F,0x027A,0x826B,0x026E,0x0264,0x8261,
  135. 0x0220,0x8225,0x822F,0x022A,0x823B,0x023E,0x0234,0x8231,0x8213,0x0216,0x021C,0x8219,0x0208,0x820D,0x8207,0x0202,
  136. };
  137. static unsigned short crc16_checksum(const unsigned char *data, unsigned int size) {
  138. unsigned int i;
  139. unsigned short sum = 0;
  140. /* HCA header/frames should always have checksum 0 (checksum(size-16b) = last 16b) */
  141. for (i = 0; i < size; i++) {
  142. sum = (sum << 8) ^ crc16_lookup_table[(sum >> 8) ^ data[i]];
  143. }
  144. return sum;
  145. }
  146. //--------------------------------------------------
  147. // Bitstream reader
  148. //--------------------------------------------------
  149. static void bitreader_init(clData *br, const void *data, int size) {
  150. br->data = data;
  151. br->size = size * 8;
  152. br->bit = 0;
  153. }
  154. static unsigned int bitreader_peek(clData *br, int bitsize) {
  155. const unsigned int bit = br->bit;
  156. const unsigned int bit_rem = bit & 7;
  157. const unsigned int size = br->size;
  158. unsigned int v = 0;
  159. unsigned int bit_offset, bit_left;
  160. if (!(bit + bitsize <= size))
  161. return v;
  162. bit_offset = bitsize + bit_rem;
  163. bit_left = size - bit;
  164. if (bit_left >= 32 && bit_offset >= 25) {
  165. static const unsigned int mask[8] = {
  166. 0xFFFFFFFF,0x7FFFFFFF,0x3FFFFFFF,0x1FFFFFFF,
  167. 0x0FFFFFFF,0x07FFFFFF,0x03FFFFFF,0x01FFFFFF
  168. };
  169. const unsigned char *data = &br->data[bit >> 3];
  170. v = data[0];
  171. v = (v << 8) | data[1];
  172. v = (v << 8) | data[2];
  173. v = (v << 8) | data[3];
  174. v &= mask[bit_rem];
  175. v >>= 32 - bit_rem - bitsize;
  176. }
  177. else if (bit_left >= 24 && bit_offset >= 17) {
  178. static const unsigned int mask[8] = {
  179. 0xFFFFFF,0x7FFFFF,0x3FFFFF,0x1FFFFF,
  180. 0x0FFFFF,0x07FFFF,0x03FFFF,0x01FFFF
  181. };
  182. const unsigned char *data = &br->data[bit >> 3];
  183. v = data[0];
  184. v = (v << 8) | data[1];
  185. v = (v << 8) | data[2];
  186. v &= mask[bit_rem];
  187. v >>= 24 - bit_rem - bitsize;
  188. }
  189. else if (bit_left >= 16 && bit_offset >= 9) {
  190. static const unsigned int mask[8] = {
  191. 0xFFFF,0x7FFF,0x3FFF,0x1FFF,0x0FFF,0x07FF,0x03FF,0x01FF
  192. };
  193. const unsigned char *data = &br->data[bit >> 3];
  194. v = data[0];
  195. v = (v << 8) | data[1];
  196. v &= mask[bit_rem];
  197. v >>= 16 - bit_rem - bitsize;
  198. }
  199. else {
  200. static const unsigned int mask[8] = {
  201. 0xFF,0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01
  202. };
  203. const unsigned char *data = &br->data[bit >> 3];
  204. v = data[0];
  205. v &= mask[bit_rem];
  206. v >>= 8 - bit_rem - bitsize;
  207. }
  208. return v;
  209. }
  210. static unsigned int bitreader_read(clData *br, int bitsize) {
  211. unsigned int v = bitreader_peek(br, bitsize);
  212. br->bit += bitsize;
  213. return v;
  214. }
  215. static void bitreader_skip(clData *br, int bitsize) {
  216. br->bit += bitsize;
  217. }
  218. //--------------------------------------------------
  219. // API/Utilities
  220. //--------------------------------------------------
  221. int clHCA_isOurFile(const void *data, unsigned int size) {
  222. clData br;
  223. unsigned int header_size = 0;
  224. if (!data || size < 0x08)
  225. return HCA_ERROR_PARAMS;
  226. bitreader_init(&br, data, 8);
  227. if ((bitreader_peek(&br, 32) & HCA_MASK) == 0x48434100) {/*'HCA\0'*/
  228. bitreader_skip(&br, 32 + 16);
  229. header_size = bitreader_read(&br, 16);
  230. }
  231. if (header_size == 0)
  232. return HCA_ERROR_HEADER;
  233. return header_size;
  234. }
  235. int clHCA_getInfo(clHCA *hca, clHCA_stInfo *info) {
  236. if (!hca || !info || !hca->is_valid)
  237. return HCA_ERROR_PARAMS;
  238. info->version = hca->version;
  239. info->headerSize = hca->header_size;
  240. info->samplingRate = hca->sample_rate;
  241. info->channelCount = hca->channels;
  242. info->blockSize = hca->frame_size;
  243. info->blockCount = hca->frame_count;
  244. info->encoderDelay = hca->encoder_delay;
  245. info->encoderPadding = hca->encoder_padding;
  246. info->loopEnabled = hca->loop_flag;
  247. info->loopStartBlock = hca->loop_start_frame;
  248. info->loopEndBlock = hca->loop_end_frame;
  249. info->loopStartDelay = hca->loop_start_delay;
  250. info->loopEndPadding = hca->loop_end_padding;
  251. info->samplesPerBlock = HCA_SAMPLES_PER_FRAME;
  252. info->comment = hca->comment;
  253. info->encryptionEnabled = hca->ciph_type == 56; /* keycode encryption */
  254. return 0;
  255. }
  256. void clHCA_ReadSamples16(clHCA *hca, signed short *samples) {
  257. const float scale = 32768.0f;
  258. float f;
  259. signed int s;
  260. unsigned int i, j, k;
  261. for (i = 0; i < HCA_SUBFRAMES_PER_FRAME; i++) {
  262. for (j = 0; j < HCA_SAMPLES_PER_SUBFRAME; j++) {
  263. for (k = 0; k < hca->channels; k++) {
  264. f = hca->channel[k].wave[i][j];
  265. //f = f * hca->rva_volume; /* rare, won't apply for now */
  266. if (f > 1.0f) {
  267. f = 1.0f;
  268. } else if (f < -1.0f) {
  269. f = -1.0f;
  270. }
  271. s = (signed int) (f * scale);
  272. if ((unsigned) (s + 0x8000) & 0xFFFF0000)
  273. s = (s >> 31) ^ 0x7FFF;
  274. *samples++ = (signed short) s;
  275. }
  276. }
  277. }
  278. }
  279. //--------------------------------------------------
  280. // Allocation and creation
  281. //--------------------------------------------------
  282. static void clHCA_constructor(clHCA *hca) {
  283. if (!hca)
  284. return;
  285. memset(hca, 0, sizeof(*hca));
  286. hca->is_valid = 0;
  287. hca->comment = 0;
  288. }
  289. static void clHCA_destructor(clHCA *hca) {
  290. if (!hca)
  291. return;
  292. free(hca->comment);
  293. hca->comment = 0;
  294. }
  295. int clHCA_sizeof() {
  296. return sizeof(clHCA);
  297. }
  298. void clHCA_clear(clHCA *hca) {
  299. clHCA_constructor(hca);
  300. }
  301. void clHCA_done(clHCA *hca) {
  302. clHCA_destructor(hca);
  303. }
  304. clHCA * clHCA_new() {
  305. clHCA *hca = (clHCA *) malloc(clHCA_sizeof());
  306. if (hca) {
  307. clHCA_constructor(hca);
  308. }
  309. return hca;
  310. }
  311. void clHCA_delete(clHCA *hca) {
  312. clHCA_destructor(hca);
  313. free(hca);
  314. }
  315. //--------------------------------------------------
  316. // ATH
  317. //--------------------------------------------------
  318. /* Base ATH (Absolute Threshold of Hearing) curve (for 41856hz).
  319. * May be a slight modification of the standard Painter & Spanias ATH curve formula. */
  320. static const unsigned char ath_base_curve[656] = {
  321. 0x78,0x5F,0x56,0x51,0x4E,0x4C,0x4B,0x49,0x48,0x48,0x47,0x46,0x46,0x45,0x45,0x45,
  322. 0x44,0x44,0x44,0x44,0x43,0x43,0x43,0x43,0x43,0x43,0x42,0x42,0x42,0x42,0x42,0x42,
  323. 0x42,0x42,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x40,0x40,0x40,0x40,
  324. 0x40,0x40,0x40,0x40,0x40,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  325. 0x3F,0x3F,0x3F,0x3E,0x3E,0x3E,0x3E,0x3E,0x3E,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,
  326. 0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,
  327. 0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,
  328. 0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,
  329. 0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3E,0x3E,0x3E,0x3E,0x3E,0x3E,0x3E,0x3F,
  330. 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  331. 0x3F,0x3F,0x3F,0x3F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,
  332. 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
  333. 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
  334. 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
  335. 0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x43,0x43,0x43,
  336. 0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x44,0x44,
  337. 0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x45,0x45,0x45,0x45,
  338. 0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,
  339. 0x46,0x46,0x47,0x47,0x47,0x47,0x47,0x47,0x47,0x47,0x47,0x47,0x48,0x48,0x48,0x48,
  340. 0x48,0x48,0x48,0x48,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0x4A,0x4A,0x4A,0x4A,
  341. 0x4A,0x4A,0x4A,0x4A,0x4B,0x4B,0x4B,0x4B,0x4B,0x4B,0x4B,0x4C,0x4C,0x4C,0x4C,0x4C,
  342. 0x4C,0x4D,0x4D,0x4D,0x4D,0x4D,0x4D,0x4E,0x4E,0x4E,0x4E,0x4E,0x4E,0x4F,0x4F,0x4F,
  343. 0x4F,0x4F,0x4F,0x50,0x50,0x50,0x50,0x50,0x51,0x51,0x51,0x51,0x51,0x52,0x52,0x52,
  344. 0x52,0x52,0x53,0x53,0x53,0x53,0x54,0x54,0x54,0x54,0x54,0x55,0x55,0x55,0x55,0x56,
  345. 0x56,0x56,0x56,0x57,0x57,0x57,0x57,0x57,0x58,0x58,0x58,0x59,0x59,0x59,0x59,0x5A,
  346. 0x5A,0x5A,0x5A,0x5B,0x5B,0x5B,0x5B,0x5C,0x5C,0x5C,0x5D,0x5D,0x5D,0x5D,0x5E,0x5E,
  347. 0x5E,0x5F,0x5F,0x5F,0x60,0x60,0x60,0x61,0x61,0x61,0x61,0x62,0x62,0x62,0x63,0x63,
  348. 0x63,0x64,0x64,0x64,0x65,0x65,0x66,0x66,0x66,0x67,0x67,0x67,0x68,0x68,0x68,0x69,
  349. 0x69,0x6A,0x6A,0x6A,0x6B,0x6B,0x6B,0x6C,0x6C,0x6D,0x6D,0x6D,0x6E,0x6E,0x6F,0x6F,
  350. 0x70,0x70,0x70,0x71,0x71,0x72,0x72,0x73,0x73,0x73,0x74,0x74,0x75,0x75,0x76,0x76,
  351. 0x77,0x77,0x78,0x78,0x78,0x79,0x79,0x7A,0x7A,0x7B,0x7B,0x7C,0x7C,0x7D,0x7D,0x7E,
  352. 0x7E,0x7F,0x7F,0x80,0x80,0x81,0x81,0x82,0x83,0x83,0x84,0x84,0x85,0x85,0x86,0x86,
  353. 0x87,0x88,0x88,0x89,0x89,0x8A,0x8A,0x8B,0x8C,0x8C,0x8D,0x8D,0x8E,0x8F,0x8F,0x90,
  354. 0x90,0x91,0x92,0x92,0x93,0x94,0x94,0x95,0x95,0x96,0x97,0x97,0x98,0x99,0x99,0x9A,
  355. 0x9B,0x9B,0x9C,0x9D,0x9D,0x9E,0x9F,0xA0,0xA0,0xA1,0xA2,0xA2,0xA3,0xA4,0xA5,0xA5,
  356. 0xA6,0xA7,0xA7,0xA8,0xA9,0xAA,0xAA,0xAB,0xAC,0xAD,0xAE,0xAE,0xAF,0xB0,0xB1,0xB1,
  357. 0xB2,0xB3,0xB4,0xB5,0xB6,0xB6,0xB7,0xB8,0xB9,0xBA,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
  358. 0xC0,0xC1,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xC9,0xCA,0xCB,0xCC,0xCD,
  359. 0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,
  360. 0xDE,0xDF,0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xED,0xEE,
  361. 0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFF,0xFF,
  362. };
  363. static void ath_init0(unsigned char *ath_curve) {
  364. /* disable curve */
  365. memset(ath_curve, 0, sizeof(ath_curve[0]) * HCA_SAMPLES_PER_SUBFRAME);
  366. }
  367. static void ath_init1(unsigned char *ath_curve, unsigned int sample_rate) {
  368. unsigned int i, index;
  369. unsigned int acc = 0;
  370. /* scale ATH curve depending on frequency */
  371. for (i = 0; i < HCA_SAMPLES_PER_SUBFRAME; i++) {
  372. acc += sample_rate;
  373. index = acc >> 13;
  374. if (index >= 654) {
  375. memset(ath_curve+i, 0xFF, sizeof(ath_curve[0]) * (HCA_SAMPLES_PER_SUBFRAME - i));
  376. break;
  377. }
  378. ath_curve[i] = ath_base_curve[index];
  379. }
  380. }
  381. static int ath_init(unsigned char *ath_curve, int type, unsigned int sample_rate) {
  382. switch (type) {
  383. case 0:
  384. ath_init0(ath_curve);
  385. break;
  386. case 1:
  387. ath_init1(ath_curve, sample_rate);
  388. break;
  389. default:
  390. return HCA_ERROR_HEADER;
  391. }
  392. return 0;
  393. }
  394. //--------------------------------------------------
  395. // Encryption
  396. //--------------------------------------------------
  397. static void cipher_decrypt(unsigned char *cipher_table, unsigned char *data, int size) {
  398. unsigned int i;
  399. for (i = 0; i < size; i++) {
  400. data[i] = cipher_table[data[i]];
  401. }
  402. }
  403. static void cipher_init0(unsigned char *cipher_table) {
  404. unsigned int i;
  405. /* no encryption */
  406. for (i = 0; i < 256; i++) {
  407. cipher_table[i] = i;
  408. }
  409. }
  410. static void cipher_init1(unsigned char *cipher_table) {
  411. const int mul = 13;
  412. const int add = 11;
  413. unsigned int i, v = 0;
  414. /* keyless encryption (rare) */
  415. for (i = 1; i < 256 - 1; i++) {
  416. v = (v * mul + add) & 0xFF;
  417. if (v == 0 || v == 0xFF)
  418. v = (v * mul + add) & 0xFF;
  419. cipher_table[i] = v;
  420. }
  421. cipher_table[0] = 0;
  422. cipher_table[0xFF] = 0xFF;
  423. }
  424. static void cipher_init56_create_table(unsigned char *r, unsigned char key) {
  425. const int mul = ((key & 1) << 3) | 5;
  426. const int add = (key & 0xE) | 1;
  427. unsigned int i;
  428. key >>= 4;
  429. for (i = 0; i < 16; i++) {
  430. key = (key * mul + add) & 0xF;
  431. r[i] = key;
  432. }
  433. }
  434. static void cipher_init56(unsigned char *cipher_table, unsigned long long keycode) {
  435. unsigned char kc[8];
  436. unsigned char seed[16];
  437. unsigned char base[256], base_r[16], base_c[16];
  438. unsigned int r, c;
  439. /* 56bit keycode encryption (given as a uint64_t number, but upper 8b aren't used) */
  440. /* keycode = keycode - 1 */
  441. if (keycode != 0)
  442. keycode--;
  443. /* init keycode table */
  444. for (r = 0; r < (8-1); r++) {
  445. kc[r] = keycode & 0xFF;
  446. keycode = keycode >> 8;
  447. }
  448. /* init seed table */
  449. seed[0x00] = kc[1];
  450. seed[0x01] = kc[1] ^ kc[6];
  451. seed[0x02] = kc[2] ^ kc[3];
  452. seed[0x03] = kc[2];
  453. seed[0x04] = kc[2] ^ kc[1];
  454. seed[0x05] = kc[3] ^ kc[4];
  455. seed[0x06] = kc[3];
  456. seed[0x07] = kc[3] ^ kc[2];
  457. seed[0x08] = kc[4] ^ kc[5];
  458. seed[0x09] = kc[4];
  459. seed[0x0A] = kc[4] ^ kc[3];
  460. seed[0x0B] = kc[5] ^ kc[6];
  461. seed[0x0C] = kc[5];
  462. seed[0x0D] = kc[5] ^ kc[4];
  463. seed[0x0E] = kc[6] ^ kc[1];
  464. seed[0x0F] = kc[6];
  465. /* init base table */
  466. cipher_init56_create_table(base_r, kc[0]);
  467. for (r = 0; r < 16; r++) {
  468. unsigned char nb;
  469. cipher_init56_create_table(base_c, seed[r]);
  470. nb = base_r[r] << 4;
  471. for (c = 0; c < 16; c++) {
  472. base[r*16 + c] = nb | base_c[c]; /* combine nibbles */
  473. }
  474. }
  475. /* final shuffle table */
  476. {
  477. unsigned int i;
  478. unsigned int x = 0;
  479. unsigned int pos = 1;
  480. for (i = 0; i < 256; i++) {
  481. x = (x + 17) & 0xFF;
  482. if (base[x] != 0 && base[x] != 0xFF)
  483. cipher_table[pos++] = base[x];
  484. }
  485. cipher_table[0] = 0;
  486. cipher_table[0xFF] = 0xFF;
  487. }
  488. }
  489. static int cipher_init(unsigned char *cipher_table, int type, unsigned long long keycode) {
  490. if (type == 56 && !(keycode))
  491. type = 0;
  492. switch (type) {
  493. case 0:
  494. cipher_init0(cipher_table);
  495. break;
  496. case 1:
  497. cipher_init1(cipher_table);
  498. break;
  499. case 56:
  500. cipher_init56(cipher_table, keycode);
  501. break;
  502. default:
  503. return HCA_ERROR_HEADER;
  504. }
  505. return 0;
  506. }
  507. //--------------------------------------------------
  508. // Parse
  509. //--------------------------------------------------
  510. static unsigned int header_ceil2(unsigned int a, unsigned int b) {
  511. return (b > 0) ? (a / b + ((a % b) ? 1 : 0)) : 0;
  512. }
  513. int clHCA_DecodeHeader(clHCA *hca, const void *data, unsigned int size) {
  514. clData br;
  515. int res;
  516. if (!hca || !data)
  517. return HCA_ERROR_PARAMS;
  518. hca->is_valid = 0;
  519. if (size < 0x08)
  520. return HCA_ERROR_PARAMS;
  521. bitreader_init(&br, data, size);
  522. /* read header chunks */
  523. /* HCA base header */
  524. if ((bitreader_peek(&br, 32) & HCA_MASK) == 0x48434100) { /* "HCA\0" */
  525. bitreader_skip(&br, 32);
  526. hca->version = bitreader_read(&br, 16);
  527. hca->header_size = bitreader_read(&br, 16);
  528. #if 0 // play unknown versions anyway (confirmed to exist: v1.1/v1.2/v1.3/v2.0)
  529. if (hca->version != 0x0101 &&
  530. hca->version != 0x0102 &&
  531. hca->version != 0x0103 &&
  532. hca->version != 0x0200)
  533. return HCA_ERROR_HEADER;
  534. #endif
  535. if (size < hca->header_size)
  536. return HCA_ERROR_PARAMS;
  537. if (crc16_checksum(data,hca->header_size))
  538. return HCA_ERROR_CHECKSUM;
  539. size -= 0x08;
  540. }
  541. else {
  542. return HCA_ERROR_HEADER;
  543. }
  544. /* format info */
  545. if (size >= 0x10 && (bitreader_peek(&br, 32) & HCA_MASK) == 0x666D7400) { /* "fmt\0" */
  546. bitreader_skip(&br, 32);
  547. hca->channels = bitreader_read(&br, 8);
  548. hca->sample_rate = bitreader_read(&br, 24);
  549. hca->frame_count = bitreader_read(&br, 32);
  550. hca->encoder_delay = bitreader_read(&br, 16);
  551. hca->encoder_padding = bitreader_read(&br, 16);
  552. if (!(hca->channels >= 1 && hca->channels <= HCA_MAX_CHANNELS))
  553. return HCA_ERROR_HEADER;
  554. if (hca->frame_count == 0)
  555. return HCA_ERROR_HEADER;
  556. if (!(hca->sample_rate >= 1 && hca->sample_rate <= 0x7FFFFF)) /* encoder max seems 48000 */
  557. return HCA_ERROR_HEADER;
  558. size -= 0x10;
  559. }
  560. else {
  561. return HCA_ERROR_HEADER;
  562. }
  563. /* compression (v2.0) or decode (v1.x) info */
  564. if (size >= 0x10 && (bitreader_peek(&br, 32) & HCA_MASK) == 0x636F6D70) { /* "comp" */
  565. bitreader_skip(&br, 32);
  566. hca->frame_size = bitreader_read(&br, 16);
  567. hca->min_resolution = bitreader_read(&br, 8);
  568. hca->max_resolution = bitreader_read(&br, 8);
  569. hca->track_count = bitreader_read(&br, 8);
  570. hca->channel_config = bitreader_read(&br, 8);
  571. hca->total_band_count = bitreader_read(&br, 8);
  572. hca->base_band_count = bitreader_read(&br, 8);
  573. hca->stereo_band_count = bitreader_read(&br, 8);
  574. hca->bands_per_hfr_group = bitreader_read(&br, 8);
  575. hca->reserved1 = bitreader_read(&br, 8);
  576. hca->reserved2 = bitreader_read(&br, 8);
  577. size -= 0x10;
  578. }
  579. else if (size >= 0x0c && (bitreader_peek(&br, 32) & HCA_MASK) == 0x64656300) { /* "dec\0" */
  580. bitreader_skip(&br, 32);
  581. hca->frame_size = bitreader_read(&br, 16);
  582. hca->min_resolution = bitreader_read(&br, 8);
  583. hca->max_resolution = bitreader_read(&br, 8);
  584. hca->total_band_count = bitreader_read(&br, 8) + 1;
  585. hca->base_band_count = bitreader_read(&br, 8) + 1;
  586. hca->track_count = bitreader_read(&br, 4);
  587. hca->channel_config = bitreader_read(&br, 4);
  588. hca->stereo_type = bitreader_read(&br, 8);
  589. if (hca->stereo_type == 0)
  590. hca->base_band_count = hca->total_band_count;
  591. hca->stereo_band_count = hca->total_band_count - hca->base_band_count;
  592. hca->bands_per_hfr_group = 0;
  593. size -= 0x0c;
  594. }
  595. else {
  596. return HCA_ERROR_HEADER;
  597. }
  598. /* VBR (variable bit rate) info */
  599. if (size >= 0x08 && (bitreader_peek(&br, 32) & HCA_MASK) == 0x76627200) { /* "vbr\0" */
  600. bitreader_skip(&br, 32);
  601. hca->vbr_max_frame_size = bitreader_read(&br, 16);
  602. hca->vbr_noise_Level = bitreader_read(&br, 16);
  603. if (!(hca->frame_size == 0 && hca->vbr_max_frame_size > 8 && hca->vbr_max_frame_size <= 0x1FF))
  604. return HCA_ERROR_HEADER;
  605. size -= 0x08;
  606. }
  607. else {
  608. /* removed in v2.0, probably unused in v1.x */
  609. hca->vbr_max_frame_size = 0;
  610. hca->vbr_noise_Level = 0;
  611. }
  612. /* ATH (Absolute Threshold of Hearing) info */
  613. if (size >= 0x06 && (bitreader_peek(&br, 32) & HCA_MASK) == 0x61746800) { /* "ath\0" */
  614. bitreader_skip(&br, 32);
  615. hca->ath_type = bitreader_read(&br, 16);
  616. }
  617. else {
  618. /* removed in v2.0, default in v1.x (maybe only used in v1.1, as v1.2/v1.3 set ath_type = 0) */
  619. hca->ath_type = (hca->version < 0x200) ? 1 : 0;
  620. }
  621. /* loop info */
  622. if (size >= 0x10 && (bitreader_peek(&br, 32) & HCA_MASK) == 0x6C6F6F70) { /* "loop" */
  623. bitreader_skip(&br, 32);
  624. hca->loop_start_frame = bitreader_read(&br, 32);
  625. hca->loop_end_frame = bitreader_read(&br, 32);
  626. hca->loop_start_delay = bitreader_read(&br, 16);
  627. hca->loop_end_padding = bitreader_read(&br, 16);
  628. hca->loop_flag = 1;
  629. if (!(hca->loop_start_frame >= 0 && hca->loop_start_frame <= hca->loop_end_frame
  630. && hca->loop_end_frame < hca->frame_count))
  631. return HCA_ERROR_HEADER;
  632. size -= 0x10;
  633. }
  634. else {
  635. hca->loop_start_frame = 0;
  636. hca->loop_end_frame = 0;
  637. hca->loop_start_delay = 0;
  638. hca->loop_end_padding = 0;
  639. hca->loop_flag = 0;
  640. }
  641. /* cipher/encryption info */
  642. if (size >= 0x06 && (bitreader_peek(&br, 32) & HCA_MASK) == 0x63697068) { /* "ciph" */
  643. bitreader_skip(&br, 32);
  644. hca->ciph_type = bitreader_read(&br, 16);
  645. if (!(hca->ciph_type == 0 || hca->ciph_type == 1 || hca->ciph_type == 56))
  646. return HCA_ERROR_HEADER;
  647. size -= 0x06;
  648. }
  649. else {
  650. hca->ciph_type = 0;
  651. }
  652. /* RVA (relative volume adjustment) info */
  653. if (size >= 0x08 && (bitreader_peek(&br, 32) & HCA_MASK) == 0x72766100) { /* "rva\0" */
  654. union {
  655. unsigned int i;
  656. float f;
  657. } rva_volume_cast;
  658. bitreader_skip(&br, 32);
  659. rva_volume_cast.i = bitreader_read(&br, 32);
  660. hca->rva_volume = rva_volume_cast.f;
  661. size -= 0x08;
  662. } else {
  663. hca->rva_volume = 1;
  664. }
  665. /* comment */
  666. if (size >= 0x05 && (bitreader_peek(&br, 32) & HCA_MASK) == 0x636F6D6D) {/* "comm" */
  667. unsigned int i;
  668. char *temp;
  669. bitreader_skip(&br, 32);
  670. hca->comment_len = bitreader_read(&br, 8);
  671. if (hca->comment_len > size)
  672. return HCA_ERROR_HEADER;
  673. temp = realloc(hca->comment, hca->comment_len + 1);
  674. if (!temp)
  675. return HCA_ERROR_HEADER;
  676. hca->comment = temp;
  677. for (i = 0; i < hca->comment_len; ++i)
  678. hca->comment[i] = bitreader_read(&br, 8);
  679. hca->comment[i] = '\0'; /* should be null terminated but make sure */
  680. size -= 0x05 + hca->comment_len;
  681. }
  682. else {
  683. hca->comment_len = 0;
  684. hca->comment = NULL;
  685. }
  686. /* padding info */
  687. if (size >= 0x04 && (bitreader_peek(&br, 32) & HCA_MASK) == 0x70616400) { /* "pad\0" */
  688. size -= (size - 0x02); /* fills up to header_size, sans checksum */
  689. }
  690. /* should be fully read, but allow as data buffer may be bigger than header_size */
  691. //if (size != 0x02)
  692. // return HCA_ERROR_HEADER;
  693. /* extra validations */
  694. if (!(hca->frame_size >= 0x08 && hca->frame_size <= 0xFFFF)) /* actual max seems 0x155*channels */
  695. return HCA_ERROR_HEADER; /* theoretically can be 0 if VBR (not seen) */
  696. if (!(hca->min_resolution == 1 && hca->max_resolution == 15))
  697. return HCA_ERROR_HEADER;
  698. /* inits state */
  699. if (hca->track_count == 0)
  700. hca->track_count = 1; /* default to avoid division by zero */
  701. hca->hfr_group_count = header_ceil2(
  702. hca->total_band_count - hca->base_band_count - hca->stereo_band_count,
  703. hca->bands_per_hfr_group);
  704. res = ath_init(hca->ath_curve, hca->ath_type, hca->sample_rate);
  705. if (res < 0)
  706. return res;
  707. res = cipher_init(hca->cipher_table, hca->ciph_type, hca->keycode);
  708. if (res < 0)
  709. return res;
  710. /* init channels */
  711. {
  712. int channel_types[HCA_MAX_CHANNELS] = {0};
  713. unsigned int i, channels_per_track;
  714. channels_per_track = hca->channels / hca->track_count;
  715. if (hca->stereo_band_count && channels_per_track > 1) {
  716. int *ct = channel_types;
  717. for (i = 0; i < hca->track_count; i++, ct += channels_per_track) {
  718. switch (channels_per_track) {
  719. case 2:
  720. ct[0] = STEREO_PRIMARY;
  721. ct[1] = STEREO_SECONDARY;
  722. break;
  723. case 3:
  724. ct[0] = STEREO_PRIMARY;
  725. ct[1] = STEREO_SECONDARY;
  726. ct[2] = DISCRETE;
  727. break;
  728. case 4:
  729. ct[0] = STEREO_PRIMARY;
  730. ct[1] = 2;
  731. if (hca->channel_config == 0) {
  732. ct[2] = STEREO_PRIMARY;
  733. ct[3] = STEREO_SECONDARY;
  734. } else {
  735. ct[2] = DISCRETE;
  736. ct[3] = DISCRETE;
  737. }
  738. break;
  739. case 5:
  740. ct[0] = STEREO_PRIMARY;
  741. ct[1] = STEREO_SECONDARY;
  742. ct[2] = DISCRETE;
  743. if (hca->channel_config <= 2) {
  744. ct[3] = STEREO_PRIMARY;
  745. ct[4] = STEREO_SECONDARY;
  746. } else {
  747. ct[3] = DISCRETE;
  748. ct[4] = DISCRETE;
  749. }
  750. break;
  751. case 6:
  752. ct[0] = STEREO_PRIMARY;
  753. ct[1] = STEREO_SECONDARY;
  754. ct[2] = DISCRETE;
  755. ct[3] = DISCRETE;
  756. ct[4] = STEREO_PRIMARY;
  757. ct[5] = STEREO_SECONDARY;
  758. break;
  759. case 7:
  760. ct[0] = STEREO_PRIMARY;
  761. ct[1] = STEREO_SECONDARY;
  762. ct[2] = DISCRETE;
  763. ct[3] = DISCRETE;
  764. ct[4] = STEREO_PRIMARY;
  765. ct[5] = STEREO_SECONDARY;
  766. ct[6] = DISCRETE;
  767. break;
  768. case 8:
  769. ct[0] = STEREO_PRIMARY;
  770. ct[1] = STEREO_SECONDARY;
  771. ct[2] = DISCRETE;
  772. ct[3] = DISCRETE;
  773. ct[4] = STEREO_PRIMARY;
  774. ct[5] = STEREO_SECONDARY;
  775. ct[6] = STEREO_PRIMARY;
  776. ct[7] = STEREO_SECONDARY;
  777. break;
  778. }
  779. }
  780. }
  781. memset(hca->channel, 0, sizeof(hca->channel));
  782. for (i = 0; i < hca->channels; i++) {
  783. hca->channel[i].type = channel_types[i];
  784. hca->channel[i].coded_scalefactor_count = (channel_types[i] != 2) ?
  785. hca->base_band_count + hca->stereo_band_count :
  786. hca->base_band_count;
  787. hca->channel[i].hfr_scales = &hca->channel[i].scalefactors[hca->base_band_count + hca->stereo_band_count];
  788. }
  789. }
  790. /* clHCA is correctly initialized and decoder state reset
  791. * (keycode is not changed between calls) */
  792. hca->is_valid = 1;
  793. return 0;
  794. }
  795. void clHCA_SetKey(clHCA *hca, unsigned long long keycode) {
  796. if (!hca)
  797. return;
  798. hca->keycode = keycode;
  799. /* May be called even if clHCA is not valid (header not parsed), as the
  800. * key will be used during DecodeHeader ciph init. If header was already
  801. * parsed reinitializes the decryption table using the new key. */
  802. if (hca->is_valid) {
  803. /* ignore error since it can't really fail */
  804. cipher_init(hca->cipher_table, hca->ciph_type, hca->keycode);
  805. }
  806. }
  807. int clHCA_TestBlock(clHCA *hca, void *data, unsigned int size) {
  808. const int frame_samples = HCA_SUBFRAMES_PER_FRAME * HCA_SAMPLES_PER_SUBFRAME;
  809. const float scale = 32768.0f;
  810. unsigned int ch, sf, s;
  811. int status;
  812. int clips = 0, blanks = 0, channel_blanks[HCA_MAX_CHANNELS] = {0};
  813. /* first blocks can be empty/silent, check all bytes but sync/crc */
  814. {
  815. int i;
  816. int is_empty = 1;
  817. const unsigned char *buf = data;
  818. for (i = 2; i < size - 0x02; i++) {
  819. if (buf[i] != 0) {
  820. is_empty = 0;
  821. break;
  822. }
  823. }
  824. if (is_empty) {
  825. return 0;
  826. }
  827. }
  828. /* return if decode fails (happens often with wrong keys due to bad bitstream values) */
  829. status = clHCA_DecodeBlock(hca, data, size);
  830. if (status < 0)
  831. return -1;
  832. /* check decode results as bad keys may still get here */
  833. for (ch = 0; ch < hca->channels; ch++) {
  834. for (sf = 0; sf < HCA_SUBFRAMES_PER_FRAME; sf++) {
  835. for (s = 0; s < HCA_SAMPLES_PER_SUBFRAME; s++) {
  836. float fsample = hca->channel[ch].wave[sf][s];
  837. if (fsample > 1.0f || fsample < -1.0f) { //improve?
  838. clips++;
  839. }
  840. else {
  841. signed int psample = (signed int) (fsample * scale);
  842. if (psample == 0 || psample == -1) {
  843. blanks++;
  844. channel_blanks[ch]++;
  845. }
  846. }
  847. }
  848. }
  849. }
  850. /* the more clips the less likely block was correctly decrypted */
  851. if (clips == 1)
  852. clips++; /* signal not full score */
  853. if (clips > 1)
  854. return clips;
  855. /* if block is silent result is not useful */
  856. if (blanks == hca->channels * frame_samples)
  857. return 0;
  858. /* some bad keys make left channel null and right normal enough (due to joint stereo stuff);
  859. * it's possible real keys could do this but don't give full marks just in case */
  860. if (hca->channels >= 2) {
  861. /* only check main L/R, other channels like BL/BR are probably not useful */
  862. if (channel_blanks[0] == frame_samples && channel_blanks[1] != frame_samples) /* maybe should check max/min values? */
  863. return 3;
  864. }
  865. /* block may be correct (but wrong keys can get this too and should test more blocks) */
  866. return 1;
  867. }
  868. void clHCA_DecodeReset(clHCA * hca) {
  869. unsigned int i;
  870. if (!hca || !hca->is_valid)
  871. return;
  872. for (i = 0; i < hca->channels; i++) {
  873. stChannel *ch = &hca->channel[i];
  874. /* most values get overwritten during decode */
  875. //memset(ch->intensity, 0, sizeof(ch->intensity[0]) * HCA_SUBFRAMES_PER_FRAME);
  876. //memset(ch->scalefactors, 0, sizeof(ch->scalefactors[0]) * HCA_SAMPLES_PER_SUBFRAME);
  877. //memset(ch->resolution, 0, sizeof(ch->resolution[0]) * HCA_SAMPLES_PER_SUBFRAME);
  878. //memset(ch->gain, 0, sizeof(ch->gain[0]) * HCA_SAMPLES_PER_SUBFRAME);
  879. //memset(ch->spectra, 0, sizeof(ch->spectra[0]) * HCA_SAMPLES_PER_SUBFRAME);
  880. //memset(ch->temp, 0, sizeof(ch->temp[0]) * HCA_SAMPLES_PER_SUBFRAME);
  881. //memset(ch->dct, 0, sizeof(ch->dct[0]) * HCA_SAMPLES_PER_SUBFRAME);
  882. memset(ch->imdct_previous, 0, sizeof(ch->imdct_previous[0]) * HCA_SAMPLES_PER_SUBFRAME);
  883. //memset(ch->wave, 0, sizeof(ch->wave[0][0]) * HCA_SUBFRAMES_PER_FRAME * HCA_SUBFRAMES_PER_FRAME);
  884. }
  885. }
  886. //--------------------------------------------------
  887. // Decode
  888. //--------------------------------------------------
  889. static int decode1_unpack_channel(stChannel *ch, clData *br,
  890. unsigned int hfr_group_count, unsigned int packed_noise_level, const unsigned char *ath_curve);
  891. static void decode2_dequantize_coefficients(stChannel *ch, clData *br);
  892. static void decode3_reconstruct_high_frequency(stChannel *ch,
  893. unsigned int hfr_group_count, unsigned int bands_per_hfr_group,
  894. unsigned int stereo_band_count, unsigned int base_band_count, unsigned int total_band_count);
  895. static void decode4_apply_intensity_stereo(stChannel *ch, int subframe,
  896. unsigned int usable_band_count, unsigned int base_band_count, unsigned int stereo_band_count);
  897. static void decoder5_run_imdct(stChannel *ch, int subframe);
  898. int clHCA_DecodeBlock(clHCA *hca, void *data, unsigned int size) {
  899. clData br;
  900. unsigned short sync;
  901. unsigned int subframe, ch;
  902. if (!data || !hca || !hca->is_valid)
  903. return HCA_ERROR_PARAMS;
  904. if (size < hca->frame_size)
  905. return HCA_ERROR_PARAMS;
  906. bitreader_init(&br, data, hca->frame_size);
  907. /* test sync (not encrypted) */
  908. sync = bitreader_read(&br, 16);
  909. if (sync != 0xFFFF)
  910. return HCA_ERROR_SYNC;
  911. if (crc16_checksum(data, hca->frame_size))
  912. return HCA_ERROR_CHECKSUM;
  913. cipher_decrypt(hca->cipher_table, data, hca->frame_size);
  914. /* unpack frame values */
  915. {
  916. unsigned int frame_acceptable_noise_level = bitreader_read(&br, 9);
  917. unsigned int frame_evaluation_boundary = bitreader_read(&br, 7);
  918. unsigned int packed_noise_level = (frame_acceptable_noise_level << 8) - frame_evaluation_boundary;
  919. for (ch = 0; ch < hca->channels; ch++) {
  920. int unpack = decode1_unpack_channel(&hca->channel[ch], &br,
  921. hca->hfr_group_count, packed_noise_level, hca->ath_curve);
  922. if (unpack < 0)
  923. return unpack;
  924. }
  925. }
  926. for (subframe = 0; subframe < HCA_SUBFRAMES_PER_FRAME; subframe++) {
  927. /* unpack channel data and get dequantized spectra */
  928. for (ch = 0; ch < hca->channels; ch++){
  929. decode2_dequantize_coefficients(&hca->channel[ch], &br);
  930. }
  931. /* restore missing bands from spectra 1 */
  932. for (ch = 0; ch < hca->channels; ch++) {
  933. decode3_reconstruct_high_frequency(&hca->channel[ch],
  934. hca->hfr_group_count, hca->bands_per_hfr_group,
  935. hca->stereo_band_count, hca->base_band_count, hca->total_band_count);
  936. }
  937. /* restore missing bands from spectra 2 */
  938. for (ch = 0; ch < hca->channels - 1; ch++) {
  939. decode4_apply_intensity_stereo(&hca->channel[ch], subframe,
  940. hca->total_band_count, hca->base_band_count, hca->stereo_band_count);
  941. }
  942. /* apply imdct */
  943. for (ch = 0; ch < hca->channels; ch++) {
  944. decoder5_run_imdct(&hca->channel[ch], subframe);
  945. }
  946. }
  947. /* should read all frame sans checksum (16b) at most */
  948. /* one frame was found to read up to 14b left (cross referenced with CRI's tools),
  949. * perhaps some encoding hiccup [World of Final Fantasy Maxima (Switch) am_ev21_0170 video],
  950. * though this validation makes more sense when testing keys and isn't normally done on decode */
  951. if (br.bit + 14 > br.size) { /* relax validation a bit for that case */
  952. return HCA_ERROR_BITREADER;
  953. }
  954. return 0;
  955. }
  956. //--------------------------------------------------
  957. // Decode 1st step
  958. //--------------------------------------------------
  959. static const unsigned char decode1_scale_to_resolution_curve[64] = {
  960. 0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0D,0x0D,
  961. 0x0D,0x0D,0x0D,0x0D,0x0C,0x0C,0x0C,0x0C,
  962. 0x0C,0x0C,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,
  963. 0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x09,
  964. 0x09,0x09,0x09,0x09,0x09,0x08,0x08,0x08,
  965. 0x08,0x08,0x08,0x07,0x06,0x06,0x05,0x04,
  966. 0x04,0x04,0x03,0x03,0x03,0x02,0x02,0x02,
  967. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  968. /* for v1.x indexes after 56 are different, but can't be used anyway */
  969. //0x02,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  970. };
  971. /* scalefactor-to-scaling table, generated from sqrt(128) * (2^(53/128))^(scale_factor - 63) */
  972. static const unsigned int decode1_dequantizer_scaling_table_int[64] = {
  973. 0x342A8D26,0x34633F89,0x3497657D,0x34C9B9BE,0x35066491,0x353311C4,0x356E9910,0x359EF532,
  974. 0x35D3CCF1,0x360D1ADF,0x363C034A,0x367A83B3,0x36A6E595,0x36DE60F5,0x371426FF,0x3745672A,
  975. 0x37838359,0x37AF3B79,0x37E97C38,0x381B8D3A,0x384F4319,0x388A14D5,0x38B7FBF0,0x38F5257D,
  976. 0x3923520F,0x39599D16,0x3990FA4D,0x39C12C4D,0x3A00B1ED,0x3A2B7A3A,0x3A647B6D,0x3A9837F0,
  977. 0x3ACAD226,0x3B071F62,0x3B340AAF,0x3B6FE4BA,0x3B9FD228,0x3BD4F35B,0x3C0DDF04,0x3C3D08A4,
  978. 0x3C7BDFED,0x3CA7CD94,0x3CDF9613,0x3D14F4F0,0x3D467991,0x3D843A29,0x3DB02F0E,0x3DEAC0C7,
  979. 0x3E1C6573,0x3E506334,0x3E8AD4C6,0x3EB8FBAF,0x3EF67A41,0x3F243516,0x3F5ACB94,0x3F91C3D3,
  980. 0x3FC238D2,0x400164D2,0x402C6897,0x4065B907,0x40990B88,0x40CBEC15,0x4107DB35,0x413504F3,
  981. };
  982. static const float *decode1_dequantizer_scaling_table = (const float *)decode1_dequantizer_scaling_table_int;
  983. static const unsigned int decode1_quantizer_step_size_int[16] = {
  984. 0x00000000,0x3F2AAAAB,0x3ECCCCCD,0x3E924925,0x3E638E39,0x3E3A2E8C,0x3E1D89D9,0x3E088889,
  985. 0x3D842108,0x3D020821,0x3C810204,0x3C008081,0x3B804020,0x3B002008,0x3A801002,0x3A000801,
  986. };
  987. static const float *decode1_quantizer_step_size = (const float *)decode1_quantizer_step_size_int;
  988. static int decode1_unpack_channel(stChannel *ch, clData *br,
  989. unsigned int hfr_group_count, unsigned int packed_noise_level, const unsigned char *ath_curve) {
  990. unsigned int i;
  991. const unsigned int csf_count = ch->coded_scalefactor_count;
  992. /* read scalefactors */
  993. {
  994. /* scale indexes to normalize dequantized coefficients */
  995. unsigned char scalefactor_delta_bits = bitreader_read(br, 3);
  996. if (scalefactor_delta_bits >= 6) {
  997. /* normal scalefactors */
  998. for (i = 0; i < csf_count; i++) {
  999. ch->scalefactors[i] = bitreader_read(br, 6);
  1000. }
  1001. }
  1002. else if (scalefactor_delta_bits > 0) {
  1003. /* delta scalefactors */
  1004. const unsigned char expected_delta = (1 << scalefactor_delta_bits) - 1;
  1005. const unsigned char extra_delta = expected_delta >> 1;
  1006. unsigned char scalefactor_prev = bitreader_read(br, 6);
  1007. ch->scalefactors[0] = scalefactor_prev;
  1008. for (i = 1; i < csf_count; i++) {
  1009. unsigned char delta = bitreader_read(br, scalefactor_delta_bits);
  1010. if (delta != expected_delta) {
  1011. /* may happen with bad keycodes, scalefactors must be 6b indexes */
  1012. int scalefactor_test = (int)scalefactor_prev + ((int)delta - (int)extra_delta);
  1013. if (scalefactor_test < 0 || scalefactor_test >= 64) {
  1014. return HCA_ERROR_UNPACK;
  1015. }
  1016. scalefactor_prev += delta - extra_delta;
  1017. } else {
  1018. scalefactor_prev = bitreader_read(br, 6);
  1019. }
  1020. ch->scalefactors[i] = scalefactor_prev;
  1021. }
  1022. }
  1023. else {
  1024. /* no scalefactors */
  1025. memset(ch->scalefactors, 0, sizeof(ch->scalefactors[0]) * HCA_SAMPLES_PER_SUBFRAME);
  1026. }
  1027. }
  1028. if (ch->type == STEREO_SECONDARY) {
  1029. /* read intensity */
  1030. unsigned char intensity_value = bitreader_peek(br, 4);
  1031. ch->intensity[0] = intensity_value;
  1032. if (intensity_value < 15) {
  1033. for (i = 0; i < HCA_SUBFRAMES_PER_FRAME; i++) {
  1034. ch->intensity[i] = bitreader_read(br, 4);
  1035. }
  1036. }
  1037. /* 15 may be an invalid value? */
  1038. //else {
  1039. // return HCA_ERROR_INSENSITY;
  1040. //}
  1041. }
  1042. else {
  1043. /* read hfr scalefactors */
  1044. for (i = 0; i < hfr_group_count; i++) {
  1045. ch->hfr_scales[i] = bitreader_read(br, 6);
  1046. }
  1047. }
  1048. /* calculate resolutions */
  1049. {
  1050. /* resolution determines the range of values per encoded spectra,
  1051. * using codebooks for lower resolutions during dequantization */
  1052. for (i = 0; i < csf_count; i++) {
  1053. unsigned char new_resolution = 0;
  1054. unsigned char scalefactor = ch->scalefactors[i];
  1055. if (scalefactor > 0) {
  1056. int noise_level = ath_curve[i] + ((packed_noise_level + i) >> 8);
  1057. int curve_position = noise_level - ((5 * scalefactor) >> 1) + 1;
  1058. /* curve values can be simplified by clamping position to (0,58) and making
  1059. * scale_table[0] = 15, table[58] = 1 (like VGAudio does) */
  1060. if (curve_position < 0)
  1061. new_resolution = 15;
  1062. else if (curve_position >= 57)
  1063. new_resolution = 1;
  1064. else
  1065. new_resolution = decode1_scale_to_resolution_curve[curve_position];
  1066. }
  1067. ch->resolution[i] = new_resolution;
  1068. }
  1069. memset(&ch->resolution[csf_count], 0, sizeof(ch->resolution[0]) * (HCA_SAMPLES_PER_SUBFRAME - csf_count));
  1070. }
  1071. /* calculate gain */
  1072. {
  1073. /* get actual scales to dequantize */
  1074. for (i = 0; i < csf_count; i++) {
  1075. float scalefactor_scale = decode1_dequantizer_scaling_table[ ch->scalefactors[i] ];
  1076. float resolution_scale = decode1_quantizer_step_size[ ch->resolution[i] ];
  1077. ch->gain[i] = scalefactor_scale * resolution_scale;
  1078. }
  1079. }
  1080. return 0;
  1081. }
  1082. //--------------------------------------------------
  1083. // Decode 2nd step
  1084. //--------------------------------------------------
  1085. static const unsigned char decode2_quantized_spectrum_max_bits[16] = {
  1086. 0,2,3,3,4,4,4,4,5,6,7,8,9,10,11,12
  1087. };
  1088. static const unsigned char decode2_quantized_spectrum_bits[128] = {
  1089. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1090. 1,1,2,2,0,0,0,0,0,0,0,0,0,0,0,0,
  1091. 2,2,2,2,2,2,3,3,0,0,0,0,0,0,0,0,
  1092. 2,2,3,3,3,3,3,3,0,0,0,0,0,0,0,0,
  1093. 3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,
  1094. 3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,
  1095. 3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,
  1096. 3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  1097. };
  1098. static const float decode2_quantized_spectrum_value[128] = {
  1099. +0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,
  1100. +0,+0,+1,-1,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,
  1101. +0,+0,+1,+1,-1,-1,+2,-2,+0,+0,+0,+0,+0,+0,+0,+0,
  1102. +0,+0,+1,-1,+2,-2,+3,-3,+0,+0,+0,+0,+0,+0,+0,+0,
  1103. +0,+0,+1,+1,-1,-1,+2,+2,-2,-2,+3,+3,-3,-3,+4,-4,
  1104. +0,+0,+1,+1,-1,-1,+2,+2,-2,-2,+3,-3,+4,-4,+5,-5,
  1105. +0,+0,+1,+1,-1,-1,+2,-2,+3,-3,+4,-4,+5,-5,+6,-6,
  1106. +0,+0,+1,-1,+2,-2,+3,-3,+4,-4,+5,-5,+6,-6,+7,-7,
  1107. };
  1108. static void decode2_dequantize_coefficients(stChannel *ch, clData *br) {
  1109. unsigned int i;
  1110. const unsigned int csf_count = ch->coded_scalefactor_count;
  1111. for (i = 0; i < csf_count; i++) {
  1112. float qc;
  1113. unsigned char resolution = ch->resolution[i];
  1114. unsigned char bits = decode2_quantized_spectrum_max_bits[resolution];
  1115. unsigned int code = bitreader_read(br, bits);
  1116. /* read spectral coefficients */
  1117. if (resolution < 8) {
  1118. /* use prefix codebooks for lower resolutions */
  1119. code += resolution << 4;
  1120. bitreader_skip(br, decode2_quantized_spectrum_bits[code] - bits);
  1121. qc = decode2_quantized_spectrum_value[code];
  1122. }
  1123. else {
  1124. /* parse values in sign-magnitude form (lowest bit = sign) */
  1125. int signed_code = (1 - ((code & 1) << 1)) * (code >> 1); /* move sign */
  1126. if (signed_code == 0)
  1127. bitreader_skip(br, -1); /* zero uses one less bit since it has no sign */
  1128. qc = (float)signed_code;
  1129. }
  1130. /* dequantize coef with gain */
  1131. ch->spectra[i] = ch->gain[i] * qc;
  1132. }
  1133. /* clean rest of spectra */
  1134. memset(&ch->spectra[csf_count], 0, sizeof(ch->spectra[0]) * (HCA_SAMPLES_PER_SUBFRAME - csf_count));
  1135. }
  1136. //--------------------------------------------------
  1137. // Decode 3rd step
  1138. //--------------------------------------------------
  1139. static const unsigned int decode3_scale_conversion_table_int[128] = {
  1140. 0x00000000,0x00000000,0x32A0B051,0x32D61B5E,0x330EA43A,0x333E0F68,0x337D3E0C,0x33A8B6D5,
  1141. 0x33E0CCDF,0x3415C3FF,0x34478D75,0x3484F1F6,0x34B123F6,0x34EC0719,0x351D3EDA,0x355184DF,
  1142. 0x358B95C2,0x35B9FCD2,0x35F7D0DF,0x36251958,0x365BFBB8,0x36928E72,0x36C346CD,0x370218AF,
  1143. 0x372D583F,0x3766F85B,0x3799E046,0x37CD078C,0x3808980F,0x38360094,0x38728177,0x38A18FAF,
  1144. 0x38D744FD,0x390F6A81,0x393F179A,0x397E9E11,0x39A9A15B,0x39E2055B,0x3A16942D,0x3A48A2D8,
  1145. 0x3A85AAC3,0x3AB21A32,0x3AED4F30,0x3B1E196E,0x3B52A81E,0x3B8C57CA,0x3BBAFF5B,0x3BF9295A,
  1146. 0x3C25FED7,0x3C5D2D82,0x3C935A2B,0x3CC4563F,0x3D02CD87,0x3D2E4934,0x3D68396A,0x3D9AB62B,
  1147. 0x3DCE248C,0x3E0955EE,0x3E36FD92,0x3E73D290,0x3EA27043,0x3ED87039,0x3F1031DC,0x3F40213B,
  1148. 0x3F800000,0x3FAA8D26,0x3FE33F89,0x4017657D,0x4049B9BE,0x40866491,0x40B311C4,0x40EE9910,
  1149. 0x411EF532,0x4153CCF1,0x418D1ADF,0x41BC034A,0x41FA83B3,0x4226E595,0x425E60F5,0x429426FF,
  1150. 0x42C5672A,0x43038359,0x432F3B79,0x43697C38,0x439B8D3A,0x43CF4319,0x440A14D5,0x4437FBF0,
  1151. 0x4475257D,0x44A3520F,0x44D99D16,0x4510FA4D,0x45412C4D,0x4580B1ED,0x45AB7A3A,0x45E47B6D,
  1152. 0x461837F0,0x464AD226,0x46871F62,0x46B40AAF,0x46EFE4BA,0x471FD228,0x4754F35B,0x478DDF04,
  1153. 0x47BD08A4,0x47FBDFED,0x4827CD94,0x485F9613,0x4894F4F0,0x48C67991,0x49043A29,0x49302F0E,
  1154. 0x496AC0C7,0x499C6573,0x49D06334,0x4A0AD4C6,0x4A38FBAF,0x4A767A41,0x4AA43516,0x4ADACB94,
  1155. 0x4B11C3D3,0x4B4238D2,0x4B8164D2,0x4BAC6897,0x4BE5B907,0x4C190B88,0x4C4BEC15,0x00000000,
  1156. };
  1157. static const float *decode3_scale_conversion_table = (const float *)decode3_scale_conversion_table_int;
  1158. static void decode3_reconstruct_high_frequency(stChannel *ch,
  1159. unsigned int hfr_group_count, unsigned int bands_per_hfr_group,
  1160. unsigned int stereo_band_count, unsigned int base_band_count, unsigned int total_band_count) {
  1161. if (ch->type == STEREO_SECONDARY)
  1162. return;
  1163. if (bands_per_hfr_group == 0) /* not in v1.x */
  1164. return;
  1165. {
  1166. unsigned int group, i;
  1167. unsigned int start_band = stereo_band_count + base_band_count;
  1168. unsigned int highband = start_band;
  1169. unsigned int lowband = start_band - 1;
  1170. for (group = 0; group < hfr_group_count; group++) {
  1171. for (i = 0; i < bands_per_hfr_group && highband < total_band_count; i++) {
  1172. unsigned int sc_index = ch->hfr_scales[group] - ch->scalefactors[lowband] + 64;
  1173. ch->spectra[highband] = decode3_scale_conversion_table[sc_index] * ch->spectra[lowband];
  1174. highband++;
  1175. lowband--;
  1176. }
  1177. }
  1178. ch->spectra[HCA_SAMPLES_PER_SUBFRAME - 1] = 0; /* last spectral coefficient should be 0 */
  1179. }
  1180. }
  1181. //--------------------------------------------------
  1182. // Decode 4th step
  1183. //--------------------------------------------------
  1184. static const unsigned int decode4_intensity_ratio_table_int[80] = {
  1185. 0x40000000,0x3FEDB6DB,0x3FDB6DB7,0x3FC92492,0x3FB6DB6E,0x3FA49249,0x3F924925,0x3F800000,
  1186. 0x3F5B6DB7,0x3F36DB6E,0x3F124925,0x3EDB6DB7,0x3E924925,0x3E124925,0x00000000,0x00000000,
  1187. /* v2.0 seems to define indexes over 15, but intensity is packed in 4b thus unused */
  1188. 0x00000000,0x32A0B051,0x32D61B5E,0x330EA43A,0x333E0F68,0x337D3E0C,0x33A8B6D5,0x33E0CCDF,
  1189. 0x3415C3FF,0x34478D75,0x3484F1F6,0x34B123F6,0x34EC0719,0x351D3EDA,0x355184DF,0x358B95C2,
  1190. 0x35B9FCD2,0x35F7D0DF,0x36251958,0x365BFBB8,0x36928E72,0x36C346CD,0x370218AF,0x372D583F,
  1191. 0x3766F85B,0x3799E046,0x37CD078C,0x3808980F,0x38360094,0x38728177,0x38A18FAF,0x38D744FD,
  1192. 0x390F6A81,0x393F179A,0x397E9E11,0x39A9A15B,0x39E2055B,0x3A16942D,0x3A48A2D8,0x3A85AAC3,
  1193. 0x3AB21A32,0x3AED4F30,0x3B1E196E,0x3B52A81E,0x3B8C57CA,0x3BBAFF5B,0x3BF9295A,0x3C25FED7,
  1194. 0x3C5D2D82,0x3C935A2B,0x3CC4563F,0x3D02CD87,0x3D2E4934,0x3D68396A,0x3D9AB62B,0x3DCE248C,
  1195. 0x3E0955EE,0x3E36FD92,0x3E73D290,0x3EA27043,0x3ED87039,0x3F1031DC,0x3F40213B,0x00000000,
  1196. };
  1197. static const float *decode4_intensity_ratio_table = (const float *)decode4_intensity_ratio_table_int;
  1198. static void decode4_apply_intensity_stereo(stChannel *ch_pair, int subframe,
  1199. unsigned int total_band_count, unsigned int base_band_count, unsigned int stereo_band_count) {
  1200. if (ch_pair[0].type != STEREO_PRIMARY)
  1201. return;
  1202. if (stereo_band_count == 0)
  1203. return;
  1204. {
  1205. float ratio_l = decode4_intensity_ratio_table[ ch_pair[1].intensity[subframe] ];
  1206. float ratio_r = ratio_l - 2.0f;
  1207. float *sp_l = ch_pair[0].spectra;
  1208. float *sp_r = ch_pair[1].spectra;
  1209. unsigned int band;
  1210. for (band = base_band_count; band < total_band_count; band++) {
  1211. sp_r[band] = sp_l[band] * ratio_r;
  1212. sp_l[band] = sp_l[band] * ratio_l;
  1213. }
  1214. }
  1215. }
  1216. //--------------------------------------------------
  1217. // Decode 5th step
  1218. //--------------------------------------------------
  1219. static const unsigned int decode5_sin_tables_int[7][64] = {
  1220. {
  1221. 0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,
  1222. 0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,
  1223. 0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,
  1224. 0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,
  1225. 0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,
  1226. 0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,
  1227. 0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,
  1228. 0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,
  1229. },{
  1230. 0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,
  1231. 0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,
  1232. 0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,
  1233. 0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,
  1234. 0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,
  1235. 0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,
  1236. 0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,
  1237. 0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,0x3F7B14BE,0x3F54DB31,
  1238. },{
  1239. 0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,
  1240. 0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,
  1241. 0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,
  1242. 0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,
  1243. 0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,
  1244. 0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,
  1245. 0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,
  1246. 0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,0x3F7EC46D,0x3F74FA0B,0x3F61C598,0x3F45E403,
  1247. },{
  1248. 0x3F7FB10F,0x3F7D3AAC,0x3F7853F8,0x3F710908,0x3F676BD8,0x3F5B941A,0x3F4D9F02,0x3F3DAEF9,
  1249. 0x3F7FB10F,0x3F7D3AAC,0x3F7853F8,0x3F710908,0x3F676BD8,0x3F5B941A,0x3F4D9F02,0x3F3DAEF9,
  1250. 0x3F7FB10F,0x3F7D3AAC,0x3F7853F8,0x3F710908,0x3F676BD8,0x3F5B941A,0x3F4D9F02,0x3F3DAEF9,
  1251. 0x3F7FB10F,0x3F7D3AAC,0x3F7853F8,0x3F710908,0x3F676BD8,0x3F5B941A,0x3F4D9F02,0x3F3DAEF9,
  1252. 0x3F7FB10F,0x3F7D3AAC,0x3F7853F8,0x3F710908,0x3F676BD8,0x3F5B941A,0x3F4D9F02,0x3F3DAEF9,
  1253. 0x3F7FB10F,0x3F7D3AAC,0x3F7853F8,0x3F710908,0x3F676BD8,0x3F5B941A,0x3F4D9F02,0x3F3DAEF9,
  1254. 0x3F7FB10F,0x3F7D3AAC,0x3F7853F8,0x3F710908,0x3F676BD8,0x3F5B941A,0x3F4D9F02,0x3F3DAEF9,
  1255. 0x3F7FB10F,0x3F7D3AAC,0x3F7853F8,0x3F710908,0x3F676BD8,0x3F5B941A,0x3F4D9F02,0x3F3DAEF9,
  1256. },{
  1257. 0x3F7FEC43,0x3F7F4E6D,0x3F7E1324,0x3F7C3B28,0x3F79C79D,0x3F76BA07,0x3F731447,0x3F6ED89E,
  1258. 0x3F6A09A7,0x3F64AA59,0x3F5EBE05,0x3F584853,0x3F514D3D,0x3F49D112,0x3F41D870,0x3F396842,
  1259. 0x3F7FEC43,0x3F7F4E6D,0x3F7E1324,0x3F7C3B28,0x3F79C79D,0x3F76BA07,0x3F731447,0x3F6ED89E,
  1260. 0x3F6A09A7,0x3F64AA59,0x3F5EBE05,0x3F584853,0x3F514D3D,0x3F49D112,0x3F41D870,0x3F396842,
  1261. 0x3F7FEC43,0x3F7F4E6D,0x3F7E1324,0x3F7C3B28,0x3F79C79D,0x3F76BA07,0x3F731447,0x3F6ED89E,
  1262. 0x3F6A09A7,0x3F64AA59,0x3F5EBE05,0x3F584853,0x3F514D3D,0x3F49D112,0x3F41D870,0x3F396842,
  1263. 0x3F7FEC43,0x3F7F4E6D,0x3F7E1324,0x3F7C3B28,0x3F79C79D,0x3F76BA07,0x3F731447,0x3F6ED89E,
  1264. 0x3F6A09A7,0x3F64AA59,0x3F5EBE05,0x3F584853,0x3F514D3D,0x3F49D112,0x3F41D870,0x3F396842,
  1265. },{
  1266. 0x3F7FFB11,0x3F7FD397,0x3F7F84AB,0x3F7F0E58,0x3F7E70B0,0x3F7DABCC,0x3F7CBFC9,0x3F7BACCD,
  1267. 0x3F7A7302,0x3F791298,0x3F778BC5,0x3F75DEC6,0x3F740BDD,0x3F721352,0x3F6FF573,0x3F6DB293,
  1268. 0x3F6B4B0C,0x3F68BF3C,0x3F660F88,0x3F633C5A,0x3F604621,0x3F5D2D53,0x3F59F26A,0x3F5695E5,
  1269. 0x3F531849,0x3F4F7A1F,0x3F4BBBF8,0x3F47DE65,0x3F43E200,0x3F3FC767,0x3F3B8F3B,0x3F373A23,
  1270. 0x3F7FFB11,0x3F7FD397,0x3F7F84AB,0x3F7F0E58,0x3F7E70B0,0x3F7DABCC,0x3F7CBFC9,0x3F7BACCD,
  1271. 0x3F7A7302,0x3F791298,0x3F778BC5,0x3F75DEC6,0x3F740BDD,0x3F721352,0x3F6FF573,0x3F6DB293,
  1272. 0x3F6B4B0C,0x3F68BF3C,0x3F660F88,0x3F633C5A,0x3F604621,0x3F5D2D53,0x3F59F26A,0x3F5695E5,
  1273. 0x3F531849,0x3F4F7A1F,0x3F4BBBF8,0x3F47DE65,0x3F43E200,0x3F3FC767,0x3F3B8F3B,0x3F373A23,
  1274. },{
  1275. 0x3F7FFEC4,0x3F7FF4E6,0x3F7FE129,0x3F7FC38F,0x3F7F9C18,0x3F7F6AC7,0x3F7F2F9D,0x3F7EEA9D,
  1276. 0x3F7E9BC9,0x3F7E4323,0x3F7DE0B1,0x3F7D7474,0x3F7CFE73,0x3F7C7EB0,0x3F7BF531,0x3F7B61FC,
  1277. 0x3F7AC516,0x3F7A1E84,0x3F796E4E,0x3F78B47B,0x3F77F110,0x3F772417,0x3F764D97,0x3F756D97,
  1278. 0x3F748422,0x3F73913F,0x3F7294F8,0x3F718F57,0x3F708066,0x3F6F6830,0x3F6E46BE,0x3F6D1C1D,
  1279. 0x3F6BE858,0x3F6AAB7B,0x3F696591,0x3F6816A8,0x3F66BECC,0x3F655E0B,0x3F63F473,0x3F628210,
  1280. 0x3F6106F2,0x3F5F8327,0x3F5DF6BE,0x3F5C61C7,0x3F5AC450,0x3F591E6A,0x3F577026,0x3F55B993,
  1281. 0x3F53FAC3,0x3F5233C6,0x3F5064AF,0x3F4E8D90,0x3F4CAE79,0x3F4AC77F,0x3F48D8B3,0x3F46E22A,
  1282. 0x3F44E3F5,0x3F42DE29,0x3F40D0DA,0x3F3EBC1B,0x3F3CA003,0x3F3A7CA4,0x3F385216,0x3F36206C,
  1283. }
  1284. };
  1285. static const unsigned int decode5_cos_tables_int[7][64]={
  1286. {
  1287. 0xBD0A8BD4,0x3D0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0xBD0A8BD4,0x3D0A8BD4,
  1288. 0x3D0A8BD4,0xBD0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0x3D0A8BD4,0xBD0A8BD4,
  1289. 0x3D0A8BD4,0xBD0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0x3D0A8BD4,0xBD0A8BD4,
  1290. 0xBD0A8BD4,0x3D0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0xBD0A8BD4,0x3D0A8BD4,
  1291. 0x3D0A8BD4,0xBD0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0x3D0A8BD4,0xBD0A8BD4,
  1292. 0xBD0A8BD4,0x3D0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0xBD0A8BD4,0x3D0A8BD4,
  1293. 0xBD0A8BD4,0x3D0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0xBD0A8BD4,0x3D0A8BD4,
  1294. 0x3D0A8BD4,0xBD0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0x3D0A8BD4,0xBD0A8BD4,
  1295. },{
  1296. 0xBE47C5C2,0xBF0E39DA,0x3E47C5C2,0x3F0E39DA,0x3E47C5C2,0x3F0E39DA,0xBE47C5C2,0xBF0E39DA,
  1297. 0x3E47C5C2,0x3F0E39DA,0xBE47C5C2,0xBF0E39DA,0xBE47C5C2,0xBF0E39DA,0x3E47C5C2,0x3F0E39DA,
  1298. 0x3E47C5C2,0x3F0E39DA,0xBE47C5C2,0xBF0E39DA,0xBE47C5C2,0xBF0E39DA,0x3E47C5C2,0x3F0E39DA,
  1299. 0xBE47C5C2,0xBF0E39DA,0x3E47C5C2,0x3F0E39DA,0x3E47C5C2,0x3F0E39DA,0xBE47C5C2,0xBF0E39DA,
  1300. 0x3E47C5C2,0x3F0E39DA,0xBE47C5C2,0xBF0E39DA,0xBE47C5C2,0xBF0E39DA,0x3E47C5C2,0x3F0E39DA,
  1301. 0xBE47C5C2,0xBF0E39DA,0x3E47C5C2,0x3F0E39DA,0x3E47C5C2,0x3F0E39DA,0xBE47C5C2,0xBF0E39DA,
  1302. 0xBE47C5C2,0xBF0E39DA,0x3E47C5C2,0x3F0E39DA,0x3E47C5C2,0x3F0E39DA,0xBE47C5C2,0xBF0E39DA,
  1303. 0x3E47C5C2,0x3F0E39DA,0xBE47C5C2,0xBF0E39DA,0xBE47C5C2,0xBF0E39DA,0x3E47C5C2,0x3F0E39DA,
  1304. },{
  1305. 0xBDC8BD36,0xBE94A031,0xBEF15AEA,0xBF226799,0x3DC8BD36,0x3E94A031,0x3EF15AEA,0x3F226799,
  1306. 0x3DC8BD36,0x3E94A031,0x3EF15AEA,0x3F226799,0xBDC8BD36,0xBE94A031,0xBEF15AEA,0xBF226799,
  1307. 0x3DC8BD36,0x3E94A031,0x3EF15AEA,0x3F226799,0xBDC8BD36,0xBE94A031,0xBEF15AEA,0xBF226799,
  1308. 0xBDC8BD36,0xBE94A031,0xBEF15AEA,0xBF226799,0x3DC8BD36,0x3E94A031,0x3EF15AEA,0x3F226799,
  1309. 0x3DC8BD36,0x3E94A031,0x3EF15AEA,0x3F226799,0xBDC8BD36,0xBE94A031,0xBEF15AEA,0xBF226799,
  1310. 0xBDC8BD36,0xBE94A031,0xBEF15AEA,0xBF226799,0x3DC8BD36,0x3E94A031,0x3EF15AEA,0x3F226799,
  1311. 0xBDC8BD36,0xBE94A031,0xBEF15AEA,0xBF226799,0x3DC8BD36,0x3E94A031,0x3EF15AEA,0x3F226799,
  1312. 0x3DC8BD36,0x3E94A031,0x3EF15AEA,0x3F226799,0xBDC8BD36,0xBE94A031,0xBEF15AEA,0xBF226799,
  1313. },{
  1314. 0xBD48FB30,0xBE164083,0xBE78CFCC,0xBEAC7CD4,0xBEDAE880,0xBF039C3D,0xBF187FC0,0xBF2BEB4A,
  1315. 0x3D48FB30,0x3E164083,0x3E78CFCC,0x3EAC7CD4,0x3EDAE880,0x3F039C3D,0x3F187FC0,0x3F2BEB4A,
  1316. 0x3D48FB30,0x3E164083,0x3E78CFCC,0x3EAC7CD4,0x3EDAE880,0x3F039C3D,0x3F187FC0,0x3F2BEB4A,
  1317. 0xBD48FB30,0xBE164083,0xBE78CFCC,0xBEAC7CD4,0xBEDAE880,0xBF039C3D,0xBF187FC0,0xBF2BEB4A,
  1318. 0x3D48FB30,0x3E164083,0x3E78CFCC,0x3EAC7CD4,0x3EDAE880,0x3F039C3D,0x3F187FC0,0x3F2BEB4A,
  1319. 0xBD48FB30,0xBE164083,0xBE78CFCC,0xBEAC7CD4,0xBEDAE880,0xBF039C3D,0xBF187FC0,0xBF2BEB4A,
  1320. 0xBD48FB30,0xBE164083,0xBE78CFCC,0xBEAC7CD4,0xBEDAE880,0xBF039C3D,0xBF187FC0,0xBF2BEB4A,
  1321. 0x3D48FB30,0x3E164083,0x3E78CFCC,0x3EAC7CD4,0x3EDAE880,0x3F039C3D,0x3F187FC0,0x3F2BEB4A,
  1322. },{
  1323. 0xBCC90AB0,0xBD96A905,0xBDFAB273,0xBE2F10A2,0xBE605C13,0xBE888E93,0xBEA09AE5,0xBEB8442A,
  1324. 0xBECF7BCA,0xBEE63375,0xBEFC5D27,0xBF08F59B,0xBF13682A,0xBF1D7FD1,0xBF273656,0xBF3085BB,
  1325. 0x3CC90AB0,0x3D96A905,0x3DFAB273,0x3E2F10A2,0x3E605C13,0x3E888E93,0x3EA09AE5,0x3EB8442A,
  1326. 0x3ECF7BCA,0x3EE63375,0x3EFC5D27,0x3F08F59B,0x3F13682A,0x3F1D7FD1,0x3F273656,0x3F3085BB,
  1327. 0x3CC90AB0,0x3D96A905,0x3DFAB273,0x3E2F10A2,0x3E605C13,0x3E888E93,0x3EA09AE5,0x3EB8442A,
  1328. 0x3ECF7BCA,0x3EE63375,0x3EFC5D27,0x3F08F59B,0x3F13682A,0x3F1D7FD1,0x3F273656,0x3F3085BB,
  1329. 0xBCC90AB0,0xBD96A905,0xBDFAB273,0xBE2F10A2,0xBE605C13,0xBE888E93,0xBEA09AE5,0xBEB8442A,
  1330. 0xBECF7BCA,0xBEE63375,0xBEFC5D27,0xBF08F59B,0xBF13682A,0xBF1D7FD1,0xBF273656,0xBF3085BB,
  1331. },{
  1332. 0xBC490E90,0xBD16C32C,0xBD7B2B74,0xBDAFB680,0xBDE1BC2E,0xBE09CF86,0xBE22ABB6,0xBE3B6ECF,
  1333. 0xBE541501,0xBE6C9A7F,0xBE827DC0,0xBE8E9A22,0xBE9AA086,0xBEA68F12,0xBEB263EF,0xBEBE1D4A,
  1334. 0xBEC9B953,0xBED53641,0xBEE0924F,0xBEEBCBBB,0xBEF6E0CB,0xBF00E7E4,0xBF064B82,0xBF0B9A6B,
  1335. 0xBF10D3CD,0xBF15F6D9,0xBF1B02C6,0xBF1FF6CB,0xBF24D225,0xBF299415,0xBF2E3BDE,0xBF32C8C9,
  1336. 0x3C490E90,0x3D16C32C,0x3D7B2B74,0x3DAFB680,0x3DE1BC2E,0x3E09CF86,0x3E22ABB6,0x3E3B6ECF,
  1337. 0x3E541501,0x3E6C9A7F,0x3E827DC0,0x3E8E9A22,0x3E9AA086,0x3EA68F12,0x3EB263EF,0x3EBE1D4A,
  1338. 0x3EC9B953,0x3ED53641,0x3EE0924F,0x3EEBCBBB,0x3EF6E0CB,0x3F00E7E4,0x3F064B82,0x3F0B9A6B,
  1339. 0x3F10D3CD,0x3F15F6D9,0x3F1B02C6,0x3F1FF6CB,0x3F24D225,0x3F299415,0x3F2E3BDE,0x3F32C8C9,
  1340. },{
  1341. 0xBBC90F88,0xBC96C9B6,0xBCFB49BA,0xBD2FE007,0xBD621469,0xBD8A200A,0xBDA3308C,0xBDBC3AC3,
  1342. 0xBDD53DB9,0xBDEE3876,0xBE039502,0xBE1008B7,0xBE1C76DE,0xBE28DEFC,0xBE354098,0xBE419B37,
  1343. 0xBE4DEE60,0xBE5A3997,0xBE667C66,0xBE72B651,0xBE7EE6E1,0xBE8586CE,0xBE8B9507,0xBE919DDD,
  1344. 0xBE97A117,0xBE9D9E78,0xBEA395C5,0xBEA986C4,0xBEAF713A,0xBEB554EC,0xBEBB31A0,0xBEC1071E,
  1345. 0xBEC6D529,0xBECC9B8B,0xBED25A09,0xBED8106B,0xBEDDBE79,0xBEE363FA,0xBEE900B7,0xBEEE9479,
  1346. 0xBEF41F07,0xBEF9A02D,0xBEFF17B2,0xBF0242B1,0xBF04F484,0xBF07A136,0xBF0A48AD,0xBF0CEAD0,
  1347. 0xBF0F8784,0xBF121EB0,0xBF14B039,0xBF173C07,0xBF19C200,0xBF1C420C,0xBF1EBC12,0xBF212FF9,
  1348. 0xBF239DA9,0xBF26050A,0xBF286605,0xBF2AC082,0xBF2D1469,0xBF2F61A5,0xBF31A81D,0xBF33E7BC,
  1349. }
  1350. };
  1351. /* HCA window function, close to a KBD window with an alpha of around 3.82 (similar to AAC/Vorbis) */
  1352. static const unsigned int decode5_imdct_window_int[128] = {
  1353. 0x3A3504F0,0x3B0183B8,0x3B70C538,0x3BBB9268,0x3C04A809,0x3C308200,0x3C61284C,0x3C8B3F17,
  1354. 0x3CA83992,0x3CC77FBD,0x3CE91110,0x3D0677CD,0x3D198FC4,0x3D2DD35C,0x3D434643,0x3D59ECC1,
  1355. 0x3D71CBA8,0x3D85741E,0x3D92A413,0x3DA078B4,0x3DAEF522,0x3DBE1C9E,0x3DCDF27B,0x3DDE7A1D,
  1356. 0x3DEFB6ED,0x3E00D62B,0x3E0A2EDA,0x3E13E72A,0x3E1E00B1,0x3E287CF2,0x3E335D55,0x3E3EA321,
  1357. 0x3E4A4F75,0x3E56633F,0x3E62DF37,0x3E6FC3D1,0x3E7D1138,0x3E8563A2,0x3E8C72B7,0x3E93B561,
  1358. 0x3E9B2AEF,0x3EA2D26F,0x3EAAAAAB,0x3EB2B222,0x3EBAE706,0x3EC34737,0x3ECBD03D,0x3ED47F46,
  1359. 0x3EDD5128,0x3EE6425C,0x3EEF4EFF,0x3EF872D7,0x3F00D4A9,0x3F0576CA,0x3F0A1D3B,0x3F0EC548,
  1360. 0x3F136C25,0x3F180EF2,0x3F1CAAC2,0x3F213CA2,0x3F25C1A5,0x3F2A36E7,0x3F2E9998,0x3F32E705,
  1361. 0xBF371C9E,0xBF3B37FE,0xBF3F36F2,0xBF431780,0xBF46D7E6,0xBF4A76A4,0xBF4DF27C,0xBF514A6F,
  1362. 0xBF547DC5,0xBF578C03,0xBF5A74EE,0xBF5D3887,0xBF5FD707,0xBF6250DA,0xBF64A699,0xBF66D908,
  1363. 0xBF68E90E,0xBF6AD7B1,0xBF6CA611,0xBF6E5562,0xBF6FE6E7,0xBF715BEF,0xBF72B5D1,0xBF73F5E6,
  1364. 0xBF751D89,0xBF762E13,0xBF7728D7,0xBF780F20,0xBF78E234,0xBF79A34C,0xBF7A5397,0xBF7AF439,
  1365. 0xBF7B8648,0xBF7C0ACE,0xBF7C82C8,0xBF7CEF26,0xBF7D50CB,0xBF7DA88E,0xBF7DF737,0xBF7E3D86,
  1366. 0xBF7E7C2A,0xBF7EB3CC,0xBF7EE507,0xBF7F106C,0xBF7F3683,0xBF7F57CA,0xBF7F74B6,0xBF7F8DB6,
  1367. 0xBF7FA32E,0xBF7FB57B,0xBF7FC4F6,0xBF7FD1ED,0xBF7FDCAD,0xBF7FE579,0xBF7FEC90,0xBF7FF22E,
  1368. 0xBF7FF688,0xBF7FF9D0,0xBF7FFC32,0xBF7FFDDA,0xBF7FFEED,0xBF7FFF8F,0xBF7FFFDF,0xBF7FFFFC,
  1369. };
  1370. static const float *decode5_imdct_window = (const float *)decode5_imdct_window_int;
  1371. static void decoder5_run_imdct(stChannel *ch, int subframe) {
  1372. static const unsigned int size = HCA_SAMPLES_PER_SUBFRAME;
  1373. static const unsigned int half = HCA_SAMPLES_PER_SUBFRAME / 2;
  1374. static const unsigned int mdct_bits = HCA_MDCT_BITS;
  1375. /* apply DCT-IV to dequantized spectra */
  1376. {
  1377. unsigned int i, j, k;
  1378. unsigned int count1a, count2a, count1b, count2b;
  1379. const float *temp1a, *temp1b;
  1380. float *temp2a, *temp2b;
  1381. /* this is all too crafty for me to simplify, see VGAudio (Mdct.Dct4) */
  1382. temp1a = ch->spectra;
  1383. temp2a = ch->temp;
  1384. count1a = 1;
  1385. count2a = half;
  1386. for (i = 0; i < mdct_bits; i++) {
  1387. float *swap;
  1388. float *d1 = &temp2a[0];
  1389. float *d2 = &temp2a[count2a];
  1390. for (j = 0; j < count1a; j++) {
  1391. for (k = 0; k < count2a; k++) {
  1392. float a = *(temp1a++);
  1393. float b = *(temp1a++);
  1394. *(d1++) = b + a;
  1395. *(d2++) = a - b;
  1396. }
  1397. d1 += count2a;
  1398. d2 += count2a;
  1399. }
  1400. swap = (float*) temp1a - HCA_SAMPLES_PER_SUBFRAME; /* move spectra/temp to beginning */
  1401. temp1a = temp2a;
  1402. temp2a = swap;
  1403. count1a = count1a << 1;
  1404. count2a = count2a >> 1;
  1405. }
  1406. temp1b = ch->temp;
  1407. temp2b = ch->spectra;
  1408. count1b = half;
  1409. count2b = 1;
  1410. for (i = 0; i < mdct_bits; i++) {
  1411. const float *sin_table = (const float *) decode5_sin_tables_int[i];
  1412. const float *cos_table = (const float *) decode5_cos_tables_int[i];
  1413. float *swap;
  1414. float *d1 = temp2b;
  1415. float *d2 = &temp2b[count2b * 2 - 1];
  1416. const float *s1 = &temp1b[0];
  1417. const float *s2 = &temp1b[count2b];
  1418. for (j = 0; j < count1b; j++) {
  1419. for (k = 0; k < count2b; k++) {
  1420. float a = *(s1++);
  1421. float b = *(s2++);
  1422. float sin = *(sin_table++);
  1423. float cos = *(cos_table++);
  1424. *(d1++) = a * sin - b * cos;
  1425. *(d2--) = a * cos + b * sin;
  1426. }
  1427. s1 += count2b;
  1428. s2 += count2b;
  1429. d1 += count2b;
  1430. d2 += count2b * 3;
  1431. }
  1432. swap = (float*) temp1b;
  1433. temp1b = temp2b;
  1434. temp2b = swap;
  1435. count1b = count1b >> 1;
  1436. count2b = count2b << 1;
  1437. }
  1438. /* copy dct */
  1439. /* (with the above optimization spectra is already modified, so this is redundant) */
  1440. for (i = 0; i < size; i++) {
  1441. ch->dct[i] = ch->spectra[i];
  1442. }
  1443. }
  1444. /* update output/imdct */
  1445. {
  1446. unsigned int i;
  1447. for (i = 0; i < half; i++) {
  1448. ch->wave[subframe][i] = decode5_imdct_window[i] * ch->dct[i + half] + ch->imdct_previous[i];
  1449. ch->wave[subframe][i + half] = decode5_imdct_window[i + half] * ch->dct[size - 1 - i] - ch->imdct_previous[i + half];
  1450. ch->imdct_previous[i] = decode5_imdct_window[size - 1 - i] * ch->dct[half - i - 1];
  1451. ch->imdct_previous[i + half] = decode5_imdct_window[half - i - 1] * ch->dct[i];
  1452. }
  1453. #if 0
  1454. /* over-optimized IMDCT (for reference), barely noticeable even when decoding hundred of files */
  1455. const float *imdct_window = decode5_imdct_window;
  1456. const float *dct;
  1457. float *imdct_previous;
  1458. float *wave = ch->wave[subframe];
  1459. dct = &ch->dct[half];
  1460. imdct_previous = ch->imdct_previous;
  1461. for (i = 0; i < half; i++) {
  1462. *(wave++) = *(dct++) * *(imdct_window++) + *(imdct_previous++);
  1463. }
  1464. for (i = 0; i < half; i++) {
  1465. *(wave++) = *(imdct_window++) * *(--dct) - *(imdct_previous++);
  1466. }
  1467. /* implicit: imdct_window pointer is now at end */
  1468. dct = &ch->dct[half - 1];
  1469. imdct_previous = ch->imdct_previous;
  1470. for (i = 0; i < half; i++) {
  1471. *(imdct_previous++) = *(--imdct_window) * *(dct--);
  1472. }
  1473. for (i = 0; i < half; i++) {
  1474. *(imdct_previous++) = *(--imdct_window) * *(++dct) ;
  1475. }
  1476. #endif
  1477. }
  1478. }