aboutsummaryrefslogtreecommitdiff
path: root/src/generate_codebook.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/generate_codebook.c')
-rw-r--r--src/generate_codebook.c121
1 files changed, 54 insertions, 67 deletions
diff --git a/src/generate_codebook.c b/src/generate_codebook.c
index efecd48..d9ec838 100644
--- a/src/generate_codebook.c
+++ b/src/generate_codebook.c
@@ -23,61 +23,56 @@
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
-#include <stdlib.h>
-#include <stdio.h>
#include <ctype.h>
#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
static const char usage[] =
-"Usage: %s filename array_name [filename ...]\n"
-"\tCreate C code for codebook tables.\n";
+ "Usage: %s filename array_name [filename ...]\n"
+ "\tCreate C code for codebook tables.\n";
static const char format[] =
-"The table format must be:\n"
-"\tTwo integers describing the dimensions of the codebook.\n"
-"\tThen, enough numbers to fill the specified dimensions.\n";
+ "The table format must be:\n"
+ "\tTwo integers describing the dimensions of the codebook.\n"
+ "\tThen, enough numbers to fill the specified dimensions.\n";
static const char header[] =
-"/* THIS IS A GENERATED FILE. Edit generate_codebook.c and its input */\n\n"
-"/*\n"
-" * This intermediary file and the files that used to create it are under \n"
-" * The LGPL. See the file COPYING.\n"
-" */\n\n"
-"#include \"defines.h\"\n\n";
+ "/* THIS IS A GENERATED FILE. Edit generate_codebook.c and its input */\n\n"
+ "/*\n"
+ " * This intermediary file and the files that used to create it are under "
+ "\n"
+ " * The LGPL. See the file COPYING.\n"
+ " */\n\n"
+ "#include \"defines.h\"\n\n";
struct codebook {
- unsigned int k;
- unsigned int log2m;
- unsigned int m;
- float * cb;
+ unsigned int k;
+ unsigned int log2m;
+ unsigned int m;
+ float *cb;
};
-static void
-dump_array(const struct codebook * b, int index)
-{
- int limit = b->k * b->m;
- int i;
+static void dump_array(const struct codebook *b, int index) {
+ int limit = b->k * b->m;
+ int i;
printf("#ifdef __EMBEDDED__\n");
printf("static const float codes%d[] = {\n", index);
printf("#else\n");
printf("static float codes%d[] = {\n", index);
printf("#endif\n");
- for ( i = 0; i < limit; i++ ) {
+ for (i = 0; i < limit; i++) {
printf(" %g", b->cb[i]);
- if ( i < limit - 1 )
- printf(",");
+ if (i < limit - 1) printf(",");
/* organise VQs by rows, looks prettier */
- if ( ((i+1) % b->k) == 0 )
- printf("\n");
+ if (((i + 1) % b->k) == 0) printf("\n");
}
printf("};\n");
}
-static void
-dump_structure(const struct codebook * b, int index)
-{
+static void dump_structure(const struct codebook *b, int index) {
printf(" {\n");
printf(" %d,\n", b->k);
printf(" %d,\n", (int)roundf(log(b->m) / log(2)));
@@ -86,30 +81,26 @@ dump_structure(const struct codebook * b, int index)
printf(" }");
}
-float
-get_float(FILE * in, const char * name, char * * cursor, char * buffer,
- int size)
-{
- for ( ; ; ) {
- char * s = *cursor;
- char c;
+float get_float(FILE *in, const char *name, char **cursor, char *buffer,
+ int size) {
+ for (;;) {
+ char *s = *cursor;
+ char c;
- while ( (c = *s) != '\0' && !isdigit(c) && c != '-' && c != '.' )
- s++;
+ while ((c = *s) != '\0' && !isdigit(c) && c != '-' && c != '.') s++;
/* Comments start with "#" and continue to the end of the line. */
- if ( c != '\0' && c != '#' ) {
- char * end = 0;
- float f = 0;
+ if (c != '\0' && c != '#') {
+ char *end = 0;
+ float f = 0;
f = strtod(s, &end);
- if ( end != s )
- *cursor = end;
+ if (end != s) *cursor = end;
return f;
}
- if ( fgets(buffer, size, in) == NULL ) {
+ if (fgets(buffer, size, in) == NULL) {
fprintf(stderr, "%s: Format error. %s\n", name, format);
exit(1);
}
@@ -117,45 +108,41 @@ get_float(FILE * in, const char * name, char * * cursor, char * buffer,
}
}
-static struct codebook *
-load(FILE * file, const char * name)
-{
- char line[1024];
- char * cursor = line;
- struct codebook * b = malloc(sizeof(struct codebook));
- int i;
- int size;
+static struct codebook *load(FILE *file, const char *name) {
+ char line[1024];
+ char *cursor = line;
+ struct codebook *b = malloc(sizeof(struct codebook));
+ int i;
+ int size;
*cursor = '\0';
b->k = (int)get_float(file, name, &cursor, line, sizeof(line));
- b->m = (int)get_float(file, name ,&cursor, line, sizeof(line));
+ b->m = (int)get_float(file, name, &cursor, line, sizeof(line));
size = b->k * b->m;
b->cb = (float *)malloc(size * sizeof(float));
- for ( i = 0; i < size; i++ )
+ for (i = 0; i < size; i++)
b->cb[i] = get_float(file, name, &cursor, line, sizeof(line));
return b;
}
-int
-main(int argc, char * * argv)
-{
- struct codebook * * cb = malloc(argc * sizeof(struct codebook *));
- int i;
+int main(int argc, char **argv) {
+ struct codebook **cb = malloc(argc * sizeof(struct codebook *));
+ int i;
- if ( argc < 2 ) {
+ if (argc < 2) {
fprintf(stderr, usage, argv[0]);
fprintf(stderr, format);
exit(1);
}
- for ( i = 0; i < argc - 2; i++ ) {
- FILE * in = fopen(argv[i + 2], "r");
+ for (i = 0; i < argc - 2; i++) {
+ FILE *in = fopen(argv[i + 2], "r");
- if ( in == NULL ) {
+ if (in == NULL) {
perror(argv[i + 2]);
exit(1);
}
@@ -166,19 +153,19 @@ main(int argc, char * * argv)
}
printf(header);
- for ( i = 0; i < argc - 2; i++ ) {
+ for (i = 0; i < argc - 2; i++) {
printf(" /* %s */\n", argv[i + 2]);
dump_array(cb[i], i);
}
printf("\nconst struct lsp_codebook %s[] = {\n", argv[1]);
- for ( i = 0; i < argc - 2; i++ ) {
+ for (i = 0; i < argc - 2; i++) {
printf(" /* %s */\n", argv[i + 2]);
dump_structure(cb[i], i);
printf(",\n");
}
printf(" { 0, 0, 0, 0 }\n");
printf("};\n");
- for( i = 0; i < argc - 2; i++ ){
+ for (i = 0; i < argc - 2; i++) {
free(cb[i]->cb);
free(cb[i]);
}