Advertisement
glank

tabledesign.c

Mar 7th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.50 KB | None | 0 0
  1. /* reverse engineered from tabledesign.exe, n64sdk */
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include <math.h>
  7. #include <unistd.h>
  8.  
  9. int ssnd_size;
  10. int ssnd_offset;
  11. int ssnd_pos;
  12.  
  13. FILE *aifc_open(const char *path)
  14. {
  15.   FILE *file = fopen(path, "rb");
  16.   if (!file)
  17.     return NULL;
  18.   if (fseek(file, 0x0C, SEEK_SET))
  19.     return NULL;
  20.   uint32_t buf;
  21.   while (1) {
  22.     if (fread(&buf, sizeof(buf), 1, file) < 1)
  23.       return NULL;
  24.     if (buf == 0x444E5353) /* SSND */
  25.       break;
  26.     if (fread(&buf, sizeof(buf), 1, file) < 1)
  27.       return NULL;
  28.     int offset = ((buf << 24) & 0xFF000000) |
  29.                  ((buf << 8)  & 0x00FF0000) |
  30.                  ((buf >> 8)  & 0x0000FF00) |
  31.                  ((buf >> 24) & 0x000000FF);
  32.     fseek(file, offset, SEEK_CUR);
  33.   }
  34.   if (fread(&buf, sizeof(buf), 1, file) < 1)
  35.     return NULL;
  36.   ssnd_size = ((buf << 24) & 0xFF000000) |
  37.               ((buf << 8)  & 0x00FF0000) |
  38.               ((buf >> 8)  & 0x0000FF00) |
  39.               ((buf >> 24) & 0x000000FF);
  40.   ssnd_offset = ftell(file) + 8;
  41.   ssnd_pos = 0;
  42.   fseek(file, 0, SEEK_SET);
  43.   return file;
  44. }
  45.  
  46. int aifc_get_channels(FILE *file)
  47. {
  48.   uint16_t result = -1;
  49.   int old_pos = ftell(file);
  50.   uint32_t buf;
  51.   while (1) {
  52.     if (fread(&buf, sizeof(buf), 1, file) < 1)
  53.       return result;
  54.     if (buf == 0x4D4D4F43) /* COMM */
  55.       break;
  56.     fseek(file, -3, SEEK_CUR);
  57.   }
  58.   if (!fseek(file, 4, SEEK_CUR)) {
  59.     fread(&result, sizeof(result), 1, file);
  60.     result = ((result << 8) & 0xFF00) | ((result >> 8) & 0x00FF);
  61.   }
  62.   fseek(file, old_pos, SEEK_SET);
  63.   return result;
  64. }
  65.  
  66. int aifc_get_tracks(FILE *file)
  67. {
  68.   int result = 0;
  69.   int old_pos = ftell(file);
  70.   if (!fseek(file, 0x0C, SEEK_SET)) {
  71.     uint32_t buf;
  72.     while (fread(&buf, sizeof(buf), 1, file) >= 1) {
  73.       if (buf == 0x444E5353) /* SSND */
  74.         ++result;
  75.       if (fread(&buf, sizeof(buf), 1, file) < 1)
  76.         return 0;
  77.       int offset = ((buf << 24) & 0xFF000000) |
  78.                    ((buf << 8)  & 0x00FF0000) |
  79.                    ((buf >> 8)  & 0x0000FF00) |
  80.                    ((buf >> 24) & 0x000000FF);
  81.       fseek(file, offset, SEEK_CUR);
  82.     }
  83.   }
  84.   fseek(file, old_pos, SEEK_SET);
  85.   return result;
  86. }
  87.  
  88. int aifc_get_bits(FILE *file)
  89. {
  90.   uint16_t result;
  91.   int old_pos = ftell(file);
  92.   uint32_t buf;
  93.   while (1) {
  94.     if (fread(&buf, sizeof(buf), 1, file) < 1)
  95.       return 512;
  96.     if (buf == 0x4D4D4F43) /* COMM */
  97.       break;
  98.     fseek(file, -3, SEEK_CUR);
  99.   }
  100.   if (!fseek(file, 10, SEEK_CUR)) {
  101.     fread(&result, sizeof(result), 1, file);
  102.     result = ((result << 8) & 0xFF00) | ((result >> 8) & 0x00FF);
  103.   }
  104.   fseek(file, old_pos, SEEK_SET);
  105.   return result;
  106. }
  107.  
  108. int aifc_get_samples(FILE *file)
  109. {
  110.   int result = 0;
  111.   int old_pos = ftell(file);
  112.   uint32_t buf;
  113.   while (1) {
  114.     if (fread(&buf, sizeof(buf), 1, file) < 1)
  115.       return result;
  116.     if (buf == 0x4D4D4F43) /* COMM */
  117.       break;
  118.     fseek(file, -3, SEEK_CUR);
  119.   }
  120.   if (!fseek(file, 6, SEEK_CUR)) {
  121.     fread(&buf, sizeof(buf), 1, file);
  122.     result = ((buf << 24) & 0xFF000000) |
  123.              ((buf << 8)  & 0x00FF0000) |
  124.              ((buf >> 8)  & 0x0000FF00) |
  125.              ((buf >> 24) & 0x000000FF);
  126.   }
  127.   fseek(file, old_pos, SEEK_SET);
  128.   return result;
  129. }
  130.  
  131. int aifc_load_samples(FILE *file, int16_t *dest, int n_samples)
  132. {
  133.   int16_t sample;
  134.   if (ssnd_pos + n_samples * sizeof(sample) > ssnd_size)
  135.     return 0;
  136.   int old_pos = ftell(file);
  137.   if (fseek(file, ssnd_offset + ssnd_pos, SEEK_SET))
  138.     return 0;
  139.   for (int i = 0; i < n_samples; ++i) {
  140.     if (fread(&sample, sizeof(sample), 1, file) < 1)
  141.       return 0;
  142.     dest[i] = ((sample << 8) & 0xFF00) | ((sample >> 8) & 0x00FF);
  143.   }
  144.   ssnd_pos += n_samples * sizeof(sample);
  145.   fseek(file, old_pos, SEEK_SET);
  146.   return n_samples;
  147. }
  148.  
  149. void algo0(int16_t *samples, int order, int frame_size, double *ptr_4)
  150. {
  151.   for (int i = 0; i < order + 1; ++i) {
  152.     double sum = 0.;
  153.     for (int j = 0; j < frame_size; ++j)
  154.       sum -= samples[frame_size + j - i] * samples[frame_size + j];
  155.     ptr_4[i] = sum;
  156.   }
  157. }
  158.  
  159. void algo1(int16_t *samples, int order, int frame_size, double **ptr_6)
  160. {
  161.   for (int i = 0; i < order; ++i) {
  162.     for (int j = 0; j < order; ++j) {
  163.       double sum = 0.;
  164.       for (int k = 0; k < frame_size; ++k)
  165.         sum += samples[frame_size + k - j - 1] *
  166.                samples[frame_size + k - i - 1];
  167.       ptr_6[i + 1][j + 1] = sum;
  168.     }
  169.   }
  170. }
  171.  
  172. int algo2(double **ptr_6, int order, int *ptr_7, int *unk_1)
  173. {
  174.   double *lptr_1 = malloc((order + 1) * sizeof(*lptr_1)); /* local.5 */
  175.  
  176.   for (int i = 0; i < order; ++i) {
  177.     double max = 0.;
  178.     for (int j = 0; j < order; ++j) {
  179.       double n = fabs(ptr_6[i + 1][j + 1]);
  180.       if (n > max)
  181.         max = n;
  182.     }
  183.     if (max == 0.)
  184.       return 1;
  185.     lptr_1[i + 1] = 1. / max;
  186.   }
  187.  
  188.   /* local.3 = ptr_7 + 1 */
  189.   /* local.1 = ptr_6 + 1 */
  190.   /* local.6 = ptr_6 + 1 */
  191.   /* local.4 = lptr_1 + 1 */
  192.   /* local.10  i = 1 */
  193.   for (int i = 0; i < order; ++i) {
  194.     for (int j = 0; j < i; ++j) {
  195.       double n = ptr_6[j + 1][i + 1];
  196.       for (int k = 0; k < j; ++k)
  197.         n -= ptr_6[k + 1][i + 1] * ptr_6[j + 1][k + 1];
  198.       ptr_6[j + 1][i + 1] = n;
  199.     }
  200.  
  201.     double max = 0.;
  202.     int max_j = 0;
  203.     for (int j = i; j < order; ++j) {
  204.       double n = ptr_6[j + 1][i + 1];
  205.       for (int k = 0; k < i; ++k)
  206.         n -= ptr_6[k + 1][i + 1] * ptr_6[j + 1][k + 1];
  207.       ptr_6[j + 1][i + 1] = n;
  208.       n = fabs(n) * lptr_1[j + 1];
  209.       if (n >= max) {
  210.         max = n;
  211.         max_j = j;
  212.       }
  213.     }
  214.     if (max_j != i) {
  215.       for (int j = 0; j < order; ++j) {
  216.         double n = ptr_6[max_j + 1][j + 1];
  217.         ptr_6[max_j + 1][j + 1] = ptr_6[i + 1][j + 1];
  218.         ptr_6[i + 1][j + 1] = n;
  219.       }
  220.       *unk_1 = -*unk_1;
  221.       lptr_1[max_j + 1] = lptr_1[i + 1];
  222.     }
  223.     ptr_7[i + 1] = max_j;
  224.     if (ptr_6[i + 1][i + 1] == 0.)
  225.       return 1;
  226.  
  227.     for (int j = i + 1; j < order; ++j)
  228.       ptr_6[j + 1][i + 1] *= 1. / ptr_6[i + 1][i + 1];
  229.   }
  230.  
  231.   free(lptr_1);
  232.  
  233.   double min = 10000000000.;
  234.   double max = 0.;
  235.   for (int i = 0; i < order; ++i) {
  236.     double n = fabs(ptr_6[i + 1][i + 1]);
  237.     if (n < min)
  238.       min = n;
  239.     if (max < n)
  240.       max = n;
  241.   }
  242.   if (min / max < 0.0000000001)
  243.     return 1;
  244.   return 0;
  245. }
  246.  
  247. void algo3(double **ptr_6, int order, int *ptr_7, double *ptr_4)
  248. {
  249.   int x = -1;
  250.   for (int i = 0; i < order; ++i) {
  251.     double n = ptr_4[ptr_7[i + 1] + 1];
  252.     ptr_4[ptr_7[i + 1] + 1] = ptr_4[i + 1];
  253.     if (x != -1) {
  254.       for (int j = 0; j < i - x; ++j)
  255.         n -= ptr_6[i + 1][x + j + 1] * ptr_4[x + j + 1];
  256.     }
  257.     else if (n != 0.)
  258.       x = i;
  259.     ptr_4[i + 1] = n;
  260.   }
  261.  
  262.   for (int i = order - 1; i >= 0; --i) {
  263.     double n = ptr_4[i + 1];
  264.     for (int j = 1; j < order - i; ++j)
  265.       n -= ptr_6[i + 1][i + j + 1] * ptr_4[i + j + 1];
  266.     ptr_4[i + 1] = n / ptr_6[i + 1][i + 1];
  267.   }
  268. }
  269.  
  270. int algo4(double *ptr_4, double *ptr_5, int order)
  271. {
  272.   int x = 0; /* local.2 */
  273.   double *lptr_2 = malloc((order + 1) * sizeof(*lptr_2)); /* local.5 */
  274.  
  275.   ptr_5[order] = ptr_4[order];
  276.   /* local.3 = &ptr_4[order - i] */
  277.   /* local.4 = &lptr_2[order - i - 1] */
  278.   /* ecx = &ptr_5[order - i] */
  279.   for (int i = 0; i < order - 1; ++i) {
  280.     for (int j = 0; j < order - i; ++j) {
  281.       double n = 1. - ptr_5[order - i] * ptr_5[order - i];
  282.       if (n == 0.) {
  283.         free(lptr_2);
  284.         return 1;
  285.       }
  286.       lptr_2[j] = (ptr_4[j] - ptr_4[order - i - j] * ptr_5[order - i]) / n;
  287.     }
  288.  
  289.     for (int j = 0; j < order - i; ++j)
  290.       ptr_4[j] = lptr_2[j];
  291.  
  292.     ptr_5[order - i - 1] = lptr_2[order - i - 1];
  293.     if (fabs(lptr_2[order - i - 1]) > 1.)
  294.       ++x;
  295.   }
  296.   free(lptr_2);
  297.   return x;
  298. }
  299.  
  300. void algo5(double *ptr_5, double *lptr_3, int order)
  301. {
  302.   lptr_3[0] = 1.;
  303.   for (int i = 0; i < order; ++i) {
  304.     lptr_3[i + 1] = ptr_5[i + 1];
  305.     for (int j = 0; j < i; ++j)
  306.       lptr_3[j + 1] += lptr_3[i - j] * lptr_3[i + 1];
  307.   }
  308. }
  309.  
  310. void algo6(double *pptr_8, int order, double *pptr_1)
  311. {
  312.   double **lptr_4 = malloc((order + 1) * sizeof(*lptr_4)); /* local.2 */
  313.   /* local.3 = &lptr_4[order] */
  314.   double *lptr_5 = malloc((order + 1) * sizeof(*lptr_5));
  315.   lptr_4[order] = lptr_5;
  316.   lptr_5[0] = 1.;
  317.  
  318.   for (int i = 0; i < order; ++i)
  319.     lptr_4[order][i + 1] = -pptr_8[i + 1];
  320.  
  321.   for (int i = 0; i < order; ++i) {
  322.     lptr_4[order - i - 1] = malloc((order - i) * sizeof(**lptr_4));
  323.     double n = lptr_4[order - i][order - i]; /* local.1 */
  324.     n = 1. - n * n;
  325.     for (int j = 0; j < order - i - 1; ++j) {
  326.       double m = lptr_4[order - i][order - i - j - 1] *
  327.                  lptr_4[order - i][order - i];
  328.       lptr_4[order - i - 1][j + 1] = (m + lptr_4[order - i][j + 1]) / n;
  329.     }
  330.   }
  331.  
  332.   pptr_1[0] = 1.;
  333.   for (int i = 0; i < order; ++i) {
  334.     pptr_1[i + 1] = 0.;
  335.     for (int j = 0; j < i + 1; ++j)
  336.       pptr_1[i + 1] += lptr_4[i + 1][j + 1] * pptr_1[i - j];
  337.   }
  338.  
  339.   free(lptr_4[order]);
  340.   for (int i = 0; i < order; ++i)
  341.     free(lptr_4[order - i - 1]);
  342.   free(lptr_4);
  343. }
  344.  
  345. int algo7(double *ptr_4, int order, double *ptr_5, double *pptr_1,
  346.           double *unk_2)
  347. {
  348.   double n = ptr_4[0]; /* local.4 */
  349.   pptr_1[0] = 1.;
  350.   int x = 0; /* local.2 */
  351.   for (int i = 0; i < order; ++i) {
  352.     double m = 0.; /* local.1 */
  353.     for (int j = 0; j < i; ++j)
  354.       m += ptr_4[i - j] * pptr_1[j + 1];
  355.     if (n > 0.)
  356.       pptr_1[i + 1] = -((ptr_4[i + 1] + m) / n);
  357.     else
  358.       pptr_1[i + 1] = 0.;
  359.     ptr_5[i + 1] = pptr_1[i + 1];
  360.     if (fabs(pptr_1[i + 1]) > 1.)
  361.       ++x;
  362.     for (int j = 0; j < i; ++j)
  363.       pptr_1[j + 1] += pptr_1[i - j] * pptr_1[i + 1];
  364.     n *= 1. - pptr_1[i + 1] * pptr_1[i + 1];
  365.   }
  366.   *unk_2 = n;
  367.   return x;
  368. }
  369.  
  370. void algo8(double **ptr_1, double *ptr_2, int order, int e, double n)
  371. {
  372.   for (int i = 0; i < e; ++i)
  373.     for (int j = 0; j < order + 1; ++j)
  374.       ptr_1[e + i][j] = ptr_2[j] * n + ptr_1[i][j];
  375. }
  376.  
  377. double algo10(double *pptr_1, double *pptr_8, int order)
  378. {
  379.   double *lptr_9 = malloc((order + 1) * sizeof(*lptr_9)); /* local.3 */
  380.   double *lptr_10 = malloc((order + 1) * sizeof(*lptr_10)); /* local.2 */
  381.  
  382.   algo6(pptr_8, order, lptr_9);
  383.  
  384.   for (int i = 0; i < order + 1; ++i) {
  385.     lptr_10[i] = 0.;
  386.     for (int j = 0; j < order - i + 1; ++j)
  387.       lptr_10[i] += pptr_1[i + j] * pptr_1[j];
  388.   }
  389.  
  390.   double n = lptr_10[0] * lptr_9[0];
  391.   for (int i = 0; i < order; ++i)
  392.     n += lptr_10[i + 1] * lptr_9[i + 1] * 2.;
  393.  
  394.   free(lptr_9);
  395.   free(lptr_10);
  396.  
  397.   return n;
  398. }
  399.  
  400. void algo9(double **ptr_1, int order, int e, double **ptr_8,
  401.            int x, int refine_iter, double n)
  402. {
  403.   double **lptr_6 = malloc(e * sizeof(*lptr_6)); /* local.10 */
  404.   /* local.7 = e * 4 */
  405.   for (int i = 0; i < e; ++i)
  406.     lptr_6[i] = malloc((order + 1) * sizeof(**lptr_6));
  407.   int *lptr_7 = malloc(e * sizeof(*lptr_7)); /* local.8 */
  408.   double *lptr_8 = malloc((order + 1) * sizeof(*lptr_8)); /* local.9 */
  409.  
  410.   /* esi = order */
  411.   for (int i = 0; i < refine_iter; ++i) {
  412.     for (int j = 0; j < e; ++j) {
  413.       lptr_7[j] = 0;
  414.       for (int k = 0; k < order + 1; ++k)
  415.         lptr_6[j][k] = 0.;
  416.     }
  417.  
  418.     for (int j = 0; j < x; ++j) {
  419.       int y = 0;
  420.       double m = 1000000000000000000000000000000.; /* local.5 */
  421.       for (int k = 0; k < e; ++k) {
  422.         double l = algo10(ptr_1[k], ptr_8[j], order);
  423.         if (l < m) {
  424.           m = l;
  425.           y = k;
  426.         }
  427.       }
  428.  
  429.       ++lptr_7[y];
  430.       algo6(ptr_8[j], order, lptr_8);
  431.  
  432.       for (int k = 0; k < order + 1; ++k)
  433.         lptr_6[y][k] += lptr_8[k];
  434.     }
  435.  
  436.     for (int j = 0; j < e; ++j)
  437.       if (lptr_7[j] > 0)
  438.         for (int k = 0; k < order + 1; ++k)
  439.           lptr_6[j][k] /= lptr_7[j];
  440.  
  441.     for (int j = 0; j < e; ++j) {
  442.       double m; /* local.1 */
  443.       algo7(lptr_6[j], order, lptr_8, ptr_1[j], &m);
  444.  
  445.       for (int k = 0; k < order; ++k) {
  446.         if (lptr_8[k + 1] >= 1.)
  447.           lptr_8[k + 1] = 0.9999999999;
  448.         if (lptr_8[k + 1] <= -1.)
  449.           lptr_8[k + 1] = -0.9999999999;
  450.       }
  451.  
  452.       algo5(lptr_8, ptr_1[j], order);
  453.     }
  454.   }
  455.  
  456.   free(lptr_7);
  457.   for (int i = 0; i < e; ++i)
  458.     free(lptr_6[i]);
  459.   free(lptr_6);
  460.   free(lptr_8);
  461. }
  462.  
  463. int algo11(FILE *f, double *pptr_1, int order)
  464. {
  465.   int x = 0;
  466.  
  467.   double **lptr_11 = malloc(8 * sizeof(*lptr_11)); /* local.4 */
  468.   for (int i = 0; i < 8; ++i)
  469.     lptr_11[i] = malloc(order * sizeof(**lptr_11));
  470.  
  471.   for (int i = 0; i < order; ++i) {
  472.     for (int j = 0; j < i; ++j)
  473.       lptr_11[i][j] = 0.;
  474.     for (int j = 0; j < order - i; ++j)
  475.       lptr_11[i][i + j] = -pptr_1[order - j];
  476.   }
  477.  
  478.   for (int i = 0; i < 8 - order; ++i)
  479.     for (int j = 0; j < order; ++j)
  480.       lptr_11[order + i][j] = 0.;
  481.  
  482.   for (int i = 0; i < 7; ++i)
  483.     for (int j = 0; j < order; ++j)
  484.       if (j <= i)
  485.         for (int k = 0; k < order; ++k)
  486.           lptr_11[i + 1][k] -= lptr_11[i - j][k] * pptr_1[j + 1];
  487.  
  488.   for (int i = 0; i < order; ++i) {
  489.     for (int j = 0; j < 8; ++j) {
  490.       double n = lptr_11[j][i] * 2048.;
  491.       int in;
  492.       if (n < 0.) {
  493.         in = n - 0.5;
  494.         if (in < -32768)
  495.           ++x;
  496.       }
  497.       else {
  498.         in = n + 0.5;
  499.         if (in > 32767)
  500.           ++x;
  501.       }
  502.       fprintf(f, "%5d ", in);
  503.     }
  504.     fprintf(f, "\n");
  505.   }
  506.  
  507.   for (int i = 0; i < 8; ++i)
  508.     free(lptr_11[i]);
  509.   free(lptr_11);
  510.  
  511.   return x;
  512. }
  513.  
  514. int main(int argc, char *argv[])
  515. {
  516.   /* parameters
  517.     local.18  int       frame_size;
  518.     local.9   int       refine_iter;
  519.     local.19  int       order;
  520.     local.16  int       bits;
  521.     local.7   double    thresh;
  522.   */
  523.  
  524.   /* locals
  525.     local.17  FILE     *file;
  526.     esi       double  **ptr_1;
  527.     local.10  double   *ptr_2;
  528.     edi       int16_t  *samples;
  529.     ebx       double   *ptr_4;
  530.     local.15  double   *ptr_5;
  531.     local.11  double  **ptr_6;
  532.     local.14  int      *ptr_7;
  533.     local.8   double  **ptr_8;
  534.     local.3   int       unk_1;
  535.   */
  536.  
  537.   int frame_size = 16;
  538.   int refine_iter = 2;
  539.   int order = 2;
  540.   int bits = 2;
  541.   double thresh = 10.;
  542.  
  543.   if (argc < 2) {
  544.     fprintf(stderr, "%s %s\n", argv[0],
  545.             "[-o order -s bits -t thresh -i "
  546.             "refine_iter -f frame_size] aifcfile");
  547.     exit(1);
  548.   }
  549.  
  550.   while (1) {
  551.     int c = getopt(argc, argv, "o:s:t:i:f:");
  552.     if (c == -1)
  553.       break;
  554.     switch (c) {
  555.       case 'o': {
  556.         if (sscanf(optarg, "%d", &order) != 1)
  557.           order = 2;
  558.         break;
  559.       }
  560.       case 's': {
  561.         if (sscanf(optarg, "%d", &bits) != 1)
  562.           bits = 2;
  563.         break;
  564.       }
  565.       case 't': {
  566.         if (sscanf(optarg, "%lf", &thresh) != 1)
  567.           thresh = 10.;
  568.         break;
  569.       }
  570.       case 'i': {
  571.         if (sscanf(optarg, "%d", &refine_iter) != 1)
  572.           refine_iter = 2;
  573.         break;
  574.       }
  575.       case 'f': {
  576.         if (sscanf(optarg, "%d", &frame_size) != 1)
  577.           frame_size = 16;
  578.         break;
  579.       }
  580.     }
  581.   }
  582.  
  583.   FILE *file = aifc_open(argv[optind]);
  584.   if (!file) {
  585.     fprintf(stderr, "%s: input AIFC file [%s] could not be opened.\n",
  586.             argv[0], argv[optind]);
  587.     exit(1);
  588.   }
  589.   int aifc_channels = aifc_get_channels(file);
  590.   if (aifc_channels != 1) {
  591.     fprintf(stderr,
  592.             "%s: file [%s] contains %d channels, "
  593.             "only 1 channel supported.\n",
  594.             argv[0], argv[optind], aifc_channels);
  595.     exit(1);
  596.   }
  597.   int aifc_tracks = aifc_get_tracks(file);
  598.   if (aifc_tracks != 1) {
  599.     fprintf(stderr,
  600.             "%s: file [%s] contains %d tracks, "
  601.             "only 1 track supported.\n",
  602.             argv[0], argv[optind], aifc_tracks);
  603.     exit(1);
  604.   }
  605.   int aifc_bits = aifc_get_bits(file);
  606.   if (aifc_bits != 16) {
  607.     fprintf(stderr,
  608.             "%s: file [%s] contains %d bit samples, "
  609.             "only 16 bit samples supported.\n",
  610.             argv[0], argv[optind], aifc_bits);
  611.     exit(1);
  612.   }
  613.   int n_samples = aifc_get_samples(file);
  614.  
  615.   double **ptr_1 = malloc((1 << bits) * sizeof(*ptr_1));
  616.   for (int i = 0; i < (1 << bits); ++i)
  617.     ptr_1[i] = malloc((order + 1) * sizeof(*ptr_1[i]));
  618.   double *ptr_2 = malloc((order + 1) * sizeof(*ptr_2));
  619.   int16_t *samples = malloc(frame_size * 2 * sizeof(*samples));
  620.   for (int i = 0; i < frame_size * 2; ++i)
  621.     samples[i] = 0;
  622.   double *ptr_4 = malloc((order + 1) * sizeof(*ptr_4));
  623.   double *ptr_5 = malloc((order + 1) * sizeof(*ptr_5));
  624.   double **ptr_6 = malloc((order + 1) * sizeof(*ptr_6));
  625.   for (int i = 0; i < (order + 1); ++i)
  626.     ptr_6[i] = malloc((order + 1) * sizeof(*ptr_6[i]));
  627.   int *ptr_7 = malloc((order + 1) * 2 * sizeof(*ptr_7));
  628.   double **ptr_8 = malloc(n_samples * sizeof(*ptr_8));
  629.  
  630.   int x = 0; /* local.12 */
  631.   while (aifc_load_samples(file, &samples[frame_size],
  632.                            frame_size) == frame_size)
  633.   {
  634.     int unk_1 = 1;
  635.     algo0(samples, order, frame_size, ptr_4);
  636.     if (fabs(ptr_4[0]) > thresh) {
  637.       algo1(samples, order, frame_size, ptr_6);
  638.       if (algo2(ptr_6, order, ptr_7, &unk_1) == 0) {
  639.         algo3(ptr_6, order, ptr_7, ptr_4);
  640.         ptr_4[0] = 1.;
  641.         if (algo4(ptr_4, ptr_5, order) == 0) {
  642.           double *lptr_3 = malloc((order + 1) * sizeof(*lptr_3));
  643.           lptr_3[0] = 0.;
  644.           ptr_8[x++] = lptr_3;
  645.           for (int i = 0; i < order; ++i) {
  646.             if (ptr_5[i + 1] >= 1.)
  647.               ptr_5[i + 1] = 0.9999999999;
  648.             if (ptr_5[i + 1] <= -1.)
  649.               ptr_5[i + 1] = -0.9999999999;
  650.           }
  651.           algo5(ptr_5, lptr_3, order);
  652.         }
  653.       }
  654.     }
  655.     for (int i = 0; i < frame_size; ++i)
  656.       samples[i] = samples[frame_size + i];
  657.   }
  658.  
  659.   ptr_4[0] = 1.;
  660.   for (int i = 0; i < order; ++i)
  661.     ptr_4[i + 1] = 0.;
  662.  
  663.   for (int i = 0; i < x; ++i) {
  664.     algo6(ptr_8[i], order, ptr_1[0]);
  665.     for (int j = 0; j < order; ++j)
  666.       ptr_4[j + 1] += ptr_1[0][j + 1];
  667.   }
  668.  
  669.   for (int i = 0; i < order; ++i)
  670.     ptr_4[i + 1] /= x;
  671.  
  672.   double unk_2;
  673.   algo7(ptr_4, order, ptr_5, ptr_1[0], &unk_2);
  674.  
  675.   for (int i = 0; i < order; ++i) {
  676.     if (ptr_5[i + 1] >= 1.)
  677.       ptr_5[i + 1] = 0.9999999999;
  678.     if (ptr_5[i + 1] <= -1.)
  679.       ptr_5[i + 1] = -0.9999999999;
  680.   }
  681.  
  682.   algo5(ptr_5, ptr_1[0], order);
  683.  
  684.   for (int i = 0; i < bits; ++i) {
  685.     for (int j = 0; j < order + 1; ++j)
  686.       ptr_2[j] = 0.;
  687.     ptr_2[order - 1] = -1.;
  688.     algo8(ptr_1, ptr_2, order, 1 << i, 0.01);
  689.     algo9(ptr_1, order, 1 << (i + 1), ptr_8, x, refine_iter, 0.);
  690.   }
  691.  
  692.   fprintf(stdout, "%d\n%d\n", order, 1 << bits);
  693.   int y = 0;
  694.   for (int i = 0; i < 1 << bits; ++i)
  695.     y += algo11(stdout, ptr_1[i], order);
  696.   if (y > 0)
  697.     fprintf(stderr, "There was overflow - check the table\n");
  698. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement