File: quant_tables.c

package info (click to toggle)
schroedinger 1.0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,480 kB
  • sloc: ansic: 97,380; sh: 11,238; xml: 6,509; makefile: 386
file content (328 lines) | stat: -rw-r--r-- 6,863 bytes parent folder | download | duplicates (2)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

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

#include <schroedinger/schrohistogram.h>

unsigned int
get_quant (int i)
{
  unsigned long long base;
  base = 1ULL<<(i/4);
  switch(i&3) {
    case 0:
      return 4*base;
    case 1:
      return (503829 * base + 52958) / 105917;
    case 2:
      return (665857 * base + 58854) / 117708;
    case 3:
      return (440253 * base + 32722) / 65444;
  }
  return 0;
}

unsigned int
get_offset_3_8 (int i)
{
  unsigned long long quant = get_quant(i);
  if (i == 0)
      return 1;
  return (quant * 3 + 4)/8;
}

int
get_offset_1_2 (int i)
{
  unsigned long long quant = get_quant(i);
  if (i == 0)
      return 1;
  if (i == 1)
      return 2;
  return (quant + 1)/2;
}

uint16_t
get_inv_quant (int i)
{
  int quant_factor = get_quant(i);
  int quant_shift = i/4 + 2;
  return floor(0.5 + 65536.0 / quant_factor * (1<<quant_shift));
}

int
get_factor (int i)
{
  if (i<2) return 0;
  return (0x10000 + i/2)/i;
}

static int
dequantize (int q, int quant_factor, int quant_offset)
{
  if (q == 0) return 0;
  if (q < 0) {
    return -((-q * quant_factor + quant_offset + 2)>>2);
  } else {
    return (q * quant_factor + quant_offset + 2)>>2;
  }
}

static int
quantize (int value, int quant_factor, int quant_offset)
{
  unsigned int x;

  if (value == 0) return 0;
  if (value < 0) {
    x = (-value)<<2;
    x /= quant_factor;
    value = -x;
  } else {
    x = value<<2;
    x /= quant_factor;
    value = x;
  }
  return value;
}

typedef struct _ErrorFuncInfo ErrorFuncInfo;
struct _ErrorFuncInfo {
  int quant_factor;
  int quant_offset;
  double power;
};

static double error_pow2(int x, void *priv)
{
  ErrorFuncInfo *efi = priv;
  int q;
  int value;
  int y;

  q = quantize (x, efi->quant_factor, efi->quant_offset);
  value = dequantize (q, efi->quant_factor, efi->quant_offset);

  y = abs (value - x);

  return pow (y, efi->power);
}

static int
maxbit (unsigned int x)
{
  int i;
  for(i=0;x;i++){
    x >>= 1;
  }
  return i;
}

static int onebits (int value)
{
  int i;
  int n_bits;
  int ones = 0;

  if (value < 0) value = -value;
  if (value == 0) return 0;
  value++;
  n_bits = maxbit (value);
  for(i=0;i<n_bits - 1;i++){
    ones += (value>>(n_bits - 2 - i))&1;
  }
  return ones;
}

static int zerobits (int value)
{
  int i;
  int n_bits;
  int zeros = 0;

  if (value < 0) value = -value;
  if (value == 0) return 0;
  value++;
  n_bits = maxbit (value);
  for(i=0;i<n_bits - 1;i++){
    zeros += 1^((value>>(n_bits - 2 - i))&1);
  }
  return zeros;
}

#define SHIFT 3
static int
iexpx (int x)
{
  if (x < (1<<SHIFT)) return x;

  return ((1<<SHIFT)|(x&((1<<SHIFT)-1))) << ((x>>SHIFT)-1);
}

int
main (int argc, char *argv[])
{
  int i;
  int n = 60;

  printf("\n");
  printf("#include \"config.h\"\n");
  printf("\n");
  printf("#include <schroedinger/schrotables.h>\n");
  printf("\n");

  /* schro_table_offset_3_8 */
  printf("const uint32_t schro_table_offset_3_8[%d] = {\n", n + 1);
  for(i=0;i<n;i+=4) {
    printf("  %10uu, %10uu, %10uu, %10uu,\n",
        get_offset_3_8(i),
        get_offset_3_8(i+1),
        get_offset_3_8(i+2),
        get_offset_3_8(i+3));
  }
  printf("  %10uu\n", get_offset_3_8(i));
  printf("};\n");
  printf("\n");

  /* schro_table_offset_1_2 */
  printf("const uint32_t schro_table_offset_1_2[%d] = {\n", n+1);
  for(i=0;i<n;i+=4) {
    printf("  %10uu, %10uu, %10uu, %10uu,\n",
        get_offset_1_2(i),
        get_offset_1_2(i+1),
        get_offset_1_2(i+2),
        get_offset_1_2(i+3));
  }
  printf("  %10uu\n", get_offset_1_2(i));
  printf("};\n");
  printf("\n");

  /* schro_table_quant */
  printf("const uint32_t schro_table_quant[%d] = {\n", n + 1);
  for(i=0;i<n;i+=4) {
    printf("  %10uu, %10uu, %10uu, %10uu,\n",
        get_quant(i),
        get_quant(i+1),
        get_quant(i+2),
        get_quant(i+3));
  }
  printf("  %10uu\n", get_quant(i));
  printf("};\n");
  printf("\n");

  /* schro_table_inverse_quant */
  printf("const uint16_t schro_table_inverse_quant[%d] = {\n", n + 1);
  for(i=0;i<n;i+=4) {
    printf("  %6uu, %6uu, %6uu, %6uu,\n",
        get_inv_quant(i),
        get_inv_quant(i+1),
        get_inv_quant(i+2),
        get_inv_quant(i+3));
  }
  printf("  %10uu\n", get_inv_quant(i));
  printf("};\n");
  printf("\n");

  /* schro_table_division_factor */
  printf("const uint16_t schro_table_division_factor[257] = {\n");
  for(i=0;i<256;i+=4) {
    printf("  %5u, %5u, %5u, %5u,\n",
        get_factor(i),
        get_factor(i+1),
        get_factor(i+2),
        get_factor(i+3));
  }
  printf("  %5u\n", get_factor(i));
  printf("};\n\n");

  /* schro_table_error_hist */
  printf("#ifdef ENABLE_ENCODER\n");
  printf("const double schro_table_error_hist_shift3_1_2[60][%d] = {\n",
      ((16-SHIFT)<<SHIFT));
  for(i=0;i<60;i++){
    SchroHistogramTable table;
    ErrorFuncInfo efi;
    int j;

    efi.quant_factor = get_quant(i);
    efi.quant_offset = get_offset_1_2(i);
    efi.power = 2.0;
    schro_histogram_table_generate (&table, error_pow2, &efi);

    printf("  { /* %d */\n", i);
    for(j=0;j<SCHRO_HISTOGRAM_SIZE;j++){
      if ((j&0x7)==0) printf("    ");
      printf("%g, ", table.weights[j]);
      if ((j&0x7)==0x7) printf("\n");
    }
    printf("  },\n");
  }
  printf("};\n\n");

  /* schro_table_onebits_hist */
  printf("const double schro_table_onebits_hist_shift3_1_2[60][%d] = {\n",
      ((16-SHIFT)<<SHIFT));
  for(i=0;i<60;i++){
    int quant_factor = get_quant(i);
    int quant_offset = get_offset_1_2(i);
    int j;

    printf("  { /* %d */\n", i);
    for(j=0;j<((16-SHIFT)<<SHIFT);j++){
      int kmin = iexpx(j);
      int kmax = iexpx(j+1);
      int k;
      double x = 0;
      int q;

      if ((j&0x7)==0) printf("    ");
      for(k=kmin;k<kmax;k++){
        q = quantize(abs(k), quant_factor, quant_offset);
        x += onebits(q);
      }
      if ((j>>SHIFT) > 0) {
        x *= 1.0/(1<<((j>>SHIFT)-1));
      }
      printf("%g, ", x);
      if ((j&0x7)==0x7) printf("\n");
    }
    printf("  },\n");
  }
  printf("};\n\n");

  /* schro_table_zerobits_hist */
  printf("const double schro_table_zerobits_hist_shift3_1_2[60][%d] = {\n",
      ((16-SHIFT)<<SHIFT));
  for(i=0;i<60;i++){
    int quant_factor = get_quant(i);
    int quant_offset = get_offset_1_2(i);
    int j;

    printf("  { /* %d */\n", i);
    for(j=0;j<((16-SHIFT)<<SHIFT);j++){
      int kmin = iexpx(j);
      int kmax = iexpx(j+1);
      int k;
      double x = 0;
      int q;

      if ((j&0x7)==0) printf("    ");
      for(k=kmin;k<kmax;k++){
        q = quantize(abs(k), quant_factor, quant_offset);
        x += zerobits(q);
      }
      if ((j>>SHIFT) > 0) {
        x *= 1.0/(1<<((j>>SHIFT)-1));
      }
      printf("%g, ", x);
      if ((j&0x7)==0x7) printf("\n");
    }
    printf("  },\n");
  }
  printf("};\n\n");
  printf("#endif\n");

  return 0;
}