Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* reverse engineered from tabledesign.exe, n64sdk */
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdint.h>
- #include <math.h>
- #include <unistd.h>
- int ssnd_size;
- int ssnd_offset;
- int ssnd_pos;
- FILE *aifc_open(const char *path)
- {
- FILE *file = fopen(path, "rb");
- if (!file)
- return NULL;
- if (fseek(file, 0x0C, SEEK_SET))
- return NULL;
- uint32_t buf;
- while (1) {
- if (fread(&buf, sizeof(buf), 1, file) < 1)
- return NULL;
- if (buf == 0x444E5353) /* SSND */
- break;
- if (fread(&buf, sizeof(buf), 1, file) < 1)
- return NULL;
- int offset = ((buf << 24) & 0xFF000000) |
- ((buf << 8) & 0x00FF0000) |
- ((buf >> 8) & 0x0000FF00) |
- ((buf >> 24) & 0x000000FF);
- fseek(file, offset, SEEK_CUR);
- }
- if (fread(&buf, sizeof(buf), 1, file) < 1)
- return NULL;
- ssnd_size = ((buf << 24) & 0xFF000000) |
- ((buf << 8) & 0x00FF0000) |
- ((buf >> 8) & 0x0000FF00) |
- ((buf >> 24) & 0x000000FF);
- ssnd_offset = ftell(file) + 8;
- ssnd_pos = 0;
- fseek(file, 0, SEEK_SET);
- return file;
- }
- int aifc_get_channels(FILE *file)
- {
- uint16_t result = -1;
- int old_pos = ftell(file);
- uint32_t buf;
- while (1) {
- if (fread(&buf, sizeof(buf), 1, file) < 1)
- return result;
- if (buf == 0x4D4D4F43) /* COMM */
- break;
- fseek(file, -3, SEEK_CUR);
- }
- if (!fseek(file, 4, SEEK_CUR)) {
- fread(&result, sizeof(result), 1, file);
- result = ((result << 8) & 0xFF00) | ((result >> 8) & 0x00FF);
- }
- fseek(file, old_pos, SEEK_SET);
- return result;
- }
- int aifc_get_tracks(FILE *file)
- {
- int result = 0;
- int old_pos = ftell(file);
- if (!fseek(file, 0x0C, SEEK_SET)) {
- uint32_t buf;
- while (fread(&buf, sizeof(buf), 1, file) >= 1) {
- if (buf == 0x444E5353) /* SSND */
- ++result;
- if (fread(&buf, sizeof(buf), 1, file) < 1)
- return 0;
- int offset = ((buf << 24) & 0xFF000000) |
- ((buf << 8) & 0x00FF0000) |
- ((buf >> 8) & 0x0000FF00) |
- ((buf >> 24) & 0x000000FF);
- fseek(file, offset, SEEK_CUR);
- }
- }
- fseek(file, old_pos, SEEK_SET);
- return result;
- }
- int aifc_get_bits(FILE *file)
- {
- uint16_t result;
- int old_pos = ftell(file);
- uint32_t buf;
- while (1) {
- if (fread(&buf, sizeof(buf), 1, file) < 1)
- return 512;
- if (buf == 0x4D4D4F43) /* COMM */
- break;
- fseek(file, -3, SEEK_CUR);
- }
- if (!fseek(file, 10, SEEK_CUR)) {
- fread(&result, sizeof(result), 1, file);
- result = ((result << 8) & 0xFF00) | ((result >> 8) & 0x00FF);
- }
- fseek(file, old_pos, SEEK_SET);
- return result;
- }
- int aifc_get_samples(FILE *file)
- {
- int result = 0;
- int old_pos = ftell(file);
- uint32_t buf;
- while (1) {
- if (fread(&buf, sizeof(buf), 1, file) < 1)
- return result;
- if (buf == 0x4D4D4F43) /* COMM */
- break;
- fseek(file, -3, SEEK_CUR);
- }
- if (!fseek(file, 6, SEEK_CUR)) {
- fread(&buf, sizeof(buf), 1, file);
- result = ((buf << 24) & 0xFF000000) |
- ((buf << 8) & 0x00FF0000) |
- ((buf >> 8) & 0x0000FF00) |
- ((buf >> 24) & 0x000000FF);
- }
- fseek(file, old_pos, SEEK_SET);
- return result;
- }
- int aifc_load_samples(FILE *file, int16_t *dest, int n_samples)
- {
- int16_t sample;
- if (ssnd_pos + n_samples * sizeof(sample) > ssnd_size)
- return 0;
- int old_pos = ftell(file);
- if (fseek(file, ssnd_offset + ssnd_pos, SEEK_SET))
- return 0;
- for (int i = 0; i < n_samples; ++i) {
- if (fread(&sample, sizeof(sample), 1, file) < 1)
- return 0;
- dest[i] = ((sample << 8) & 0xFF00) | ((sample >> 8) & 0x00FF);
- }
- ssnd_pos += n_samples * sizeof(sample);
- fseek(file, old_pos, SEEK_SET);
- return n_samples;
- }
- void algo0(int16_t *samples, int order, int frame_size, double *ptr_4)
- {
- for (int i = 0; i < order + 1; ++i) {
- double sum = 0.;
- for (int j = 0; j < frame_size; ++j)
- sum -= samples[frame_size + j - i] * samples[frame_size + j];
- ptr_4[i] = sum;
- }
- }
- void algo1(int16_t *samples, int order, int frame_size, double **ptr_6)
- {
- for (int i = 0; i < order; ++i) {
- for (int j = 0; j < order; ++j) {
- double sum = 0.;
- for (int k = 0; k < frame_size; ++k)
- sum += samples[frame_size + k - j - 1] *
- samples[frame_size + k - i - 1];
- ptr_6[i + 1][j + 1] = sum;
- }
- }
- }
- int algo2(double **ptr_6, int order, int *ptr_7, int *unk_1)
- {
- double *lptr_1 = malloc((order + 1) * sizeof(*lptr_1)); /* local.5 */
- for (int i = 0; i < order; ++i) {
- double max = 0.;
- for (int j = 0; j < order; ++j) {
- double n = fabs(ptr_6[i + 1][j + 1]);
- if (n > max)
- max = n;
- }
- if (max == 0.)
- return 1;
- lptr_1[i + 1] = 1. / max;
- }
- /* local.3 = ptr_7 + 1 */
- /* local.1 = ptr_6 + 1 */
- /* local.6 = ptr_6 + 1 */
- /* local.4 = lptr_1 + 1 */
- /* local.10 i = 1 */
- for (int i = 0; i < order; ++i) {
- for (int j = 0; j < i; ++j) {
- double n = ptr_6[j + 1][i + 1];
- for (int k = 0; k < j; ++k)
- n -= ptr_6[k + 1][i + 1] * ptr_6[j + 1][k + 1];
- ptr_6[j + 1][i + 1] = n;
- }
- double max = 0.;
- int max_j = 0;
- for (int j = i; j < order; ++j) {
- double n = ptr_6[j + 1][i + 1];
- for (int k = 0; k < i; ++k)
- n -= ptr_6[k + 1][i + 1] * ptr_6[j + 1][k + 1];
- ptr_6[j + 1][i + 1] = n;
- n = fabs(n) * lptr_1[j + 1];
- if (n >= max) {
- max = n;
- max_j = j;
- }
- }
- if (max_j != i) {
- for (int j = 0; j < order; ++j) {
- double n = ptr_6[max_j + 1][j + 1];
- ptr_6[max_j + 1][j + 1] = ptr_6[i + 1][j + 1];
- ptr_6[i + 1][j + 1] = n;
- }
- *unk_1 = -*unk_1;
- lptr_1[max_j + 1] = lptr_1[i + 1];
- }
- ptr_7[i + 1] = max_j;
- if (ptr_6[i + 1][i + 1] == 0.)
- return 1;
- for (int j = i + 1; j < order; ++j)
- ptr_6[j + 1][i + 1] *= 1. / ptr_6[i + 1][i + 1];
- }
- free(lptr_1);
- double min = 10000000000.;
- double max = 0.;
- for (int i = 0; i < order; ++i) {
- double n = fabs(ptr_6[i + 1][i + 1]);
- if (n < min)
- min = n;
- if (max < n)
- max = n;
- }
- if (min / max < 0.0000000001)
- return 1;
- return 0;
- }
- void algo3(double **ptr_6, int order, int *ptr_7, double *ptr_4)
- {
- int x = -1;
- for (int i = 0; i < order; ++i) {
- double n = ptr_4[ptr_7[i + 1] + 1];
- ptr_4[ptr_7[i + 1] + 1] = ptr_4[i + 1];
- if (x != -1) {
- for (int j = 0; j < i - x; ++j)
- n -= ptr_6[i + 1][x + j + 1] * ptr_4[x + j + 1];
- }
- else if (n != 0.)
- x = i;
- ptr_4[i + 1] = n;
- }
- for (int i = order - 1; i >= 0; --i) {
- double n = ptr_4[i + 1];
- for (int j = 1; j < order - i; ++j)
- n -= ptr_6[i + 1][i + j + 1] * ptr_4[i + j + 1];
- ptr_4[i + 1] = n / ptr_6[i + 1][i + 1];
- }
- }
- int algo4(double *ptr_4, double *ptr_5, int order)
- {
- int x = 0; /* local.2 */
- double *lptr_2 = malloc((order + 1) * sizeof(*lptr_2)); /* local.5 */
- ptr_5[order] = ptr_4[order];
- /* local.3 = &ptr_4[order - i] */
- /* local.4 = &lptr_2[order - i - 1] */
- /* ecx = &ptr_5[order - i] */
- for (int i = 0; i < order - 1; ++i) {
- for (int j = 0; j < order - i; ++j) {
- double n = 1. - ptr_5[order - i] * ptr_5[order - i];
- if (n == 0.) {
- free(lptr_2);
- return 1;
- }
- lptr_2[j] = (ptr_4[j] - ptr_4[order - i - j] * ptr_5[order - i]) / n;
- }
- for (int j = 0; j < order - i; ++j)
- ptr_4[j] = lptr_2[j];
- ptr_5[order - i - 1] = lptr_2[order - i - 1];
- if (fabs(lptr_2[order - i - 1]) > 1.)
- ++x;
- }
- free(lptr_2);
- return x;
- }
- void algo5(double *ptr_5, double *lptr_3, int order)
- {
- lptr_3[0] = 1.;
- for (int i = 0; i < order; ++i) {
- lptr_3[i + 1] = ptr_5[i + 1];
- for (int j = 0; j < i; ++j)
- lptr_3[j + 1] += lptr_3[i - j] * lptr_3[i + 1];
- }
- }
- void algo6(double *pptr_8, int order, double *pptr_1)
- {
- double **lptr_4 = malloc((order + 1) * sizeof(*lptr_4)); /* local.2 */
- /* local.3 = &lptr_4[order] */
- double *lptr_5 = malloc((order + 1) * sizeof(*lptr_5));
- lptr_4[order] = lptr_5;
- lptr_5[0] = 1.;
- for (int i = 0; i < order; ++i)
- lptr_4[order][i + 1] = -pptr_8[i + 1];
- for (int i = 0; i < order; ++i) {
- lptr_4[order - i - 1] = malloc((order - i) * sizeof(**lptr_4));
- double n = lptr_4[order - i][order - i]; /* local.1 */
- n = 1. - n * n;
- for (int j = 0; j < order - i - 1; ++j) {
- double m = lptr_4[order - i][order - i - j - 1] *
- lptr_4[order - i][order - i];
- lptr_4[order - i - 1][j + 1] = (m + lptr_4[order - i][j + 1]) / n;
- }
- }
- pptr_1[0] = 1.;
- for (int i = 0; i < order; ++i) {
- pptr_1[i + 1] = 0.;
- for (int j = 0; j < i + 1; ++j)
- pptr_1[i + 1] += lptr_4[i + 1][j + 1] * pptr_1[i - j];
- }
- free(lptr_4[order]);
- for (int i = 0; i < order; ++i)
- free(lptr_4[order - i - 1]);
- free(lptr_4);
- }
- int algo7(double *ptr_4, int order, double *ptr_5, double *pptr_1,
- double *unk_2)
- {
- double n = ptr_4[0]; /* local.4 */
- pptr_1[0] = 1.;
- int x = 0; /* local.2 */
- for (int i = 0; i < order; ++i) {
- double m = 0.; /* local.1 */
- for (int j = 0; j < i; ++j)
- m += ptr_4[i - j] * pptr_1[j + 1];
- if (n > 0.)
- pptr_1[i + 1] = -((ptr_4[i + 1] + m) / n);
- else
- pptr_1[i + 1] = 0.;
- ptr_5[i + 1] = pptr_1[i + 1];
- if (fabs(pptr_1[i + 1]) > 1.)
- ++x;
- for (int j = 0; j < i; ++j)
- pptr_1[j + 1] += pptr_1[i - j] * pptr_1[i + 1];
- n *= 1. - pptr_1[i + 1] * pptr_1[i + 1];
- }
- *unk_2 = n;
- return x;
- }
- void algo8(double **ptr_1, double *ptr_2, int order, int e, double n)
- {
- for (int i = 0; i < e; ++i)
- for (int j = 0; j < order + 1; ++j)
- ptr_1[e + i][j] = ptr_2[j] * n + ptr_1[i][j];
- }
- double algo10(double *pptr_1, double *pptr_8, int order)
- {
- double *lptr_9 = malloc((order + 1) * sizeof(*lptr_9)); /* local.3 */
- double *lptr_10 = malloc((order + 1) * sizeof(*lptr_10)); /* local.2 */
- algo6(pptr_8, order, lptr_9);
- for (int i = 0; i < order + 1; ++i) {
- lptr_10[i] = 0.;
- for (int j = 0; j < order - i + 1; ++j)
- lptr_10[i] += pptr_1[i + j] * pptr_1[j];
- }
- double n = lptr_10[0] * lptr_9[0];
- for (int i = 0; i < order; ++i)
- n += lptr_10[i + 1] * lptr_9[i + 1] * 2.;
- free(lptr_9);
- free(lptr_10);
- return n;
- }
- void algo9(double **ptr_1, int order, int e, double **ptr_8,
- int x, int refine_iter, double n)
- {
- double **lptr_6 = malloc(e * sizeof(*lptr_6)); /* local.10 */
- /* local.7 = e * 4 */
- for (int i = 0; i < e; ++i)
- lptr_6[i] = malloc((order + 1) * sizeof(**lptr_6));
- int *lptr_7 = malloc(e * sizeof(*lptr_7)); /* local.8 */
- double *lptr_8 = malloc((order + 1) * sizeof(*lptr_8)); /* local.9 */
- /* esi = order */
- for (int i = 0; i < refine_iter; ++i) {
- for (int j = 0; j < e; ++j) {
- lptr_7[j] = 0;
- for (int k = 0; k < order + 1; ++k)
- lptr_6[j][k] = 0.;
- }
- for (int j = 0; j < x; ++j) {
- int y = 0;
- double m = 1000000000000000000000000000000.; /* local.5 */
- for (int k = 0; k < e; ++k) {
- double l = algo10(ptr_1[k], ptr_8[j], order);
- if (l < m) {
- m = l;
- y = k;
- }
- }
- ++lptr_7[y];
- algo6(ptr_8[j], order, lptr_8);
- for (int k = 0; k < order + 1; ++k)
- lptr_6[y][k] += lptr_8[k];
- }
- for (int j = 0; j < e; ++j)
- if (lptr_7[j] > 0)
- for (int k = 0; k < order + 1; ++k)
- lptr_6[j][k] /= lptr_7[j];
- for (int j = 0; j < e; ++j) {
- double m; /* local.1 */
- algo7(lptr_6[j], order, lptr_8, ptr_1[j], &m);
- for (int k = 0; k < order; ++k) {
- if (lptr_8[k + 1] >= 1.)
- lptr_8[k + 1] = 0.9999999999;
- if (lptr_8[k + 1] <= -1.)
- lptr_8[k + 1] = -0.9999999999;
- }
- algo5(lptr_8, ptr_1[j], order);
- }
- }
- free(lptr_7);
- for (int i = 0; i < e; ++i)
- free(lptr_6[i]);
- free(lptr_6);
- free(lptr_8);
- }
- int algo11(FILE *f, double *pptr_1, int order)
- {
- int x = 0;
- double **lptr_11 = malloc(8 * sizeof(*lptr_11)); /* local.4 */
- for (int i = 0; i < 8; ++i)
- lptr_11[i] = malloc(order * sizeof(**lptr_11));
- for (int i = 0; i < order; ++i) {
- for (int j = 0; j < i; ++j)
- lptr_11[i][j] = 0.;
- for (int j = 0; j < order - i; ++j)
- lptr_11[i][i + j] = -pptr_1[order - j];
- }
- for (int i = 0; i < 8 - order; ++i)
- for (int j = 0; j < order; ++j)
- lptr_11[order + i][j] = 0.;
- for (int i = 0; i < 7; ++i)
- for (int j = 0; j < order; ++j)
- if (j <= i)
- for (int k = 0; k < order; ++k)
- lptr_11[i + 1][k] -= lptr_11[i - j][k] * pptr_1[j + 1];
- for (int i = 0; i < order; ++i) {
- for (int j = 0; j < 8; ++j) {
- double n = lptr_11[j][i] * 2048.;
- int in;
- if (n < 0.) {
- in = n - 0.5;
- if (in < -32768)
- ++x;
- }
- else {
- in = n + 0.5;
- if (in > 32767)
- ++x;
- }
- fprintf(f, "%5d ", in);
- }
- fprintf(f, "\n");
- }
- for (int i = 0; i < 8; ++i)
- free(lptr_11[i]);
- free(lptr_11);
- return x;
- }
- int main(int argc, char *argv[])
- {
- /* parameters
- local.18 int frame_size;
- local.9 int refine_iter;
- local.19 int order;
- local.16 int bits;
- local.7 double thresh;
- */
- /* locals
- local.17 FILE *file;
- esi double **ptr_1;
- local.10 double *ptr_2;
- edi int16_t *samples;
- ebx double *ptr_4;
- local.15 double *ptr_5;
- local.11 double **ptr_6;
- local.14 int *ptr_7;
- local.8 double **ptr_8;
- local.3 int unk_1;
- */
- int frame_size = 16;
- int refine_iter = 2;
- int order = 2;
- int bits = 2;
- double thresh = 10.;
- if (argc < 2) {
- fprintf(stderr, "%s %s\n", argv[0],
- "[-o order -s bits -t thresh -i "
- "refine_iter -f frame_size] aifcfile");
- exit(1);
- }
- while (1) {
- int c = getopt(argc, argv, "o:s:t:i:f:");
- if (c == -1)
- break;
- switch (c) {
- case 'o': {
- if (sscanf(optarg, "%d", &order) != 1)
- order = 2;
- break;
- }
- case 's': {
- if (sscanf(optarg, "%d", &bits) != 1)
- bits = 2;
- break;
- }
- case 't': {
- if (sscanf(optarg, "%lf", &thresh) != 1)
- thresh = 10.;
- break;
- }
- case 'i': {
- if (sscanf(optarg, "%d", &refine_iter) != 1)
- refine_iter = 2;
- break;
- }
- case 'f': {
- if (sscanf(optarg, "%d", &frame_size) != 1)
- frame_size = 16;
- break;
- }
- }
- }
- FILE *file = aifc_open(argv[optind]);
- if (!file) {
- fprintf(stderr, "%s: input AIFC file [%s] could not be opened.\n",
- argv[0], argv[optind]);
- exit(1);
- }
- int aifc_channels = aifc_get_channels(file);
- if (aifc_channels != 1) {
- fprintf(stderr,
- "%s: file [%s] contains %d channels, "
- "only 1 channel supported.\n",
- argv[0], argv[optind], aifc_channels);
- exit(1);
- }
- int aifc_tracks = aifc_get_tracks(file);
- if (aifc_tracks != 1) {
- fprintf(stderr,
- "%s: file [%s] contains %d tracks, "
- "only 1 track supported.\n",
- argv[0], argv[optind], aifc_tracks);
- exit(1);
- }
- int aifc_bits = aifc_get_bits(file);
- if (aifc_bits != 16) {
- fprintf(stderr,
- "%s: file [%s] contains %d bit samples, "
- "only 16 bit samples supported.\n",
- argv[0], argv[optind], aifc_bits);
- exit(1);
- }
- int n_samples = aifc_get_samples(file);
- double **ptr_1 = malloc((1 << bits) * sizeof(*ptr_1));
- for (int i = 0; i < (1 << bits); ++i)
- ptr_1[i] = malloc((order + 1) * sizeof(*ptr_1[i]));
- double *ptr_2 = malloc((order + 1) * sizeof(*ptr_2));
- int16_t *samples = malloc(frame_size * 2 * sizeof(*samples));
- for (int i = 0; i < frame_size * 2; ++i)
- samples[i] = 0;
- double *ptr_4 = malloc((order + 1) * sizeof(*ptr_4));
- double *ptr_5 = malloc((order + 1) * sizeof(*ptr_5));
- double **ptr_6 = malloc((order + 1) * sizeof(*ptr_6));
- for (int i = 0; i < (order + 1); ++i)
- ptr_6[i] = malloc((order + 1) * sizeof(*ptr_6[i]));
- int *ptr_7 = malloc((order + 1) * 2 * sizeof(*ptr_7));
- double **ptr_8 = malloc(n_samples * sizeof(*ptr_8));
- int x = 0; /* local.12 */
- while (aifc_load_samples(file, &samples[frame_size],
- frame_size) == frame_size)
- {
- int unk_1 = 1;
- algo0(samples, order, frame_size, ptr_4);
- if (fabs(ptr_4[0]) > thresh) {
- algo1(samples, order, frame_size, ptr_6);
- if (algo2(ptr_6, order, ptr_7, &unk_1) == 0) {
- algo3(ptr_6, order, ptr_7, ptr_4);
- ptr_4[0] = 1.;
- if (algo4(ptr_4, ptr_5, order) == 0) {
- double *lptr_3 = malloc((order + 1) * sizeof(*lptr_3));
- lptr_3[0] = 0.;
- ptr_8[x++] = lptr_3;
- for (int i = 0; i < order; ++i) {
- if (ptr_5[i + 1] >= 1.)
- ptr_5[i + 1] = 0.9999999999;
- if (ptr_5[i + 1] <= -1.)
- ptr_5[i + 1] = -0.9999999999;
- }
- algo5(ptr_5, lptr_3, order);
- }
- }
- }
- for (int i = 0; i < frame_size; ++i)
- samples[i] = samples[frame_size + i];
- }
- ptr_4[0] = 1.;
- for (int i = 0; i < order; ++i)
- ptr_4[i + 1] = 0.;
- for (int i = 0; i < x; ++i) {
- algo6(ptr_8[i], order, ptr_1[0]);
- for (int j = 0; j < order; ++j)
- ptr_4[j + 1] += ptr_1[0][j + 1];
- }
- for (int i = 0; i < order; ++i)
- ptr_4[i + 1] /= x;
- double unk_2;
- algo7(ptr_4, order, ptr_5, ptr_1[0], &unk_2);
- for (int i = 0; i < order; ++i) {
- if (ptr_5[i + 1] >= 1.)
- ptr_5[i + 1] = 0.9999999999;
- if (ptr_5[i + 1] <= -1.)
- ptr_5[i + 1] = -0.9999999999;
- }
- algo5(ptr_5, ptr_1[0], order);
- for (int i = 0; i < bits; ++i) {
- for (int j = 0; j < order + 1; ++j)
- ptr_2[j] = 0.;
- ptr_2[order - 1] = -1.;
- algo8(ptr_1, ptr_2, order, 1 << i, 0.01);
- algo9(ptr_1, order, 1 << (i + 1), ptr_8, x, refine_iter, 0.);
- }
- fprintf(stdout, "%d\n%d\n", order, 1 << bits);
- int y = 0;
- for (int i = 0; i < 1 << bits; ++i)
- y += algo11(stdout, ptr_1[i], order);
- if (y > 0)
- fprintf(stderr, "There was overflow - check the table\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement