summaryrefslogtreecommitdiff
path: root/gr-vocoder/lib/codec2/generate_codebook.c
blob: 705f29d3822c05abac4c7f55e70ff9c7bad49e77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*---------------------------------------------------------------------------*\

  FILE........: generate_codebook.c
  AUTHOR......: Bruce Perens
  DATE CREATED: 29 Sep 2010

  Generate header files containing LSP quantisers, runs at compile time.

\*---------------------------------------------------------------------------*/

/*
  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  This program is
  distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>

static const char usage[] =
"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";

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";

struct codebook {
  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;

  printf("static const float codes%d[] = {\n", index);
  for ( i = 0; i < limit; i++ ) {
    printf("  %g", b->cb[i]);
    if ( i < limit - 1 )
      printf(",");

    /* organise VQs by rows, looks prettier */
    if ( ((i+1) % b->k) == 0 )
	printf("\n");
  }
  printf("};\n");
}

static void
dump_structure(const struct codebook * b, int index)
{
  printf("  {\n");
  printf("    %d,\n", b->k);
  printf("    %g,\n", log(b->m) / log(2));
  printf("    %d,\n", b->m);
  printf("    codes%d\n", index);
  printf("  }");
}

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++;

    /* Comments start with "#" and continue to the end of the line. */
    if ( c != '\0' && c != '#' ) {
      char *	end = 0;
      float	f = 0;

      f = strtod(s, &end);

      if ( end != s )
        *cursor = end;
        return f;
    }

    if ( fgets(buffer, size, in) == NULL ) {
      fprintf(stderr, "%s: Format error. %s\n", name, format);
      exit(1);
    }
    *cursor = 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;

  *cursor = '\0';

  b->k = (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++ )
    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;

  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");

    if ( in == NULL ) {
      perror(argv[i + 2]);
      exit(1);
    }

    cb[i] = load(in, argv[i + 2]);

    fclose(in);
  }

  printf(header);
  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++ ) {
    printf("  /* %s */\n", argv[i + 2]);
    dump_structure(cb[i], i);
    printf(",\n");
  }
  printf("  { 0, 0, 0, 0 }\n");
  printf("};\n");

  return 0;
}