comparison intercom/ilbc/iLBC_encode.c @ 2:13be24d74cd2

import intercom-0.4.1
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 09:57:52 +0200
parents
children
comparison
equal deleted inserted replaced
1:9cadc470e3da 2:13be24d74cd2
1
2 /******************************************************************
3
4 iLBC Speech Coder ANSI-C Source Code
5
6 iLBC_encode.c
7
8 Copyright (C) The Internet Society (2004).
9 All Rights Reserved.
10
11 ******************************************************************/
12
13 #include <math.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "iLBC_define.h"
18 #include "LPCencode.h"
19 #include "FrameClassify.h"
20 #include "StateSearchW.h"
21 #include "StateConstructW.h"
22 #include "helpfun.h"
23 #include "constants.h"
24 #include "packing.h"
25 #include "iCBSearch.h"
26 #include "iCBConstruct.h"
27 #include "hpInput.h"
28 #include "anaFilter.h"
29 #include "syntFilter.h"
30
31 /*----------------------------------------------------------------*
32 * Initiation of encoder instance.
33 *---------------------------------------------------------------*/
34
35 short initEncode( /* (o) Number of bytes
36 encoded */
37 iLBC_Enc_Inst_t * iLBCenc_inst, /* (i/o) Encoder instance */
38 int mode /* (i) frame size mode */
39 )
40 {
41 iLBCenc_inst->mode = mode;
42 if (mode == 30) {
43 iLBCenc_inst->blockl = BLOCKL_30MS;
44 iLBCenc_inst->nsub = NSUB_30MS;
45 iLBCenc_inst->nasub = NASUB_30MS;
46 iLBCenc_inst->lpc_n = LPC_N_30MS;
47 iLBCenc_inst->no_of_bytes = NO_OF_BYTES_30MS;
48 iLBCenc_inst->no_of_words = NO_OF_WORDS_30MS;
49
50
51
52
53
54 iLBCenc_inst->state_short_len = STATE_SHORT_LEN_30MS;
55 /* ULP init */
56 iLBCenc_inst->ULP_inst = &ULP_30msTbl;
57 } else if (mode == 20) {
58 iLBCenc_inst->blockl = BLOCKL_20MS;
59 iLBCenc_inst->nsub = NSUB_20MS;
60 iLBCenc_inst->nasub = NASUB_20MS;
61 iLBCenc_inst->lpc_n = LPC_N_20MS;
62 iLBCenc_inst->no_of_bytes = NO_OF_BYTES_20MS;
63 iLBCenc_inst->no_of_words = NO_OF_WORDS_20MS;
64 iLBCenc_inst->state_short_len = STATE_SHORT_LEN_20MS;
65 /* ULP init */
66 iLBCenc_inst->ULP_inst = &ULP_20msTbl;
67 } else {
68 exit(2);
69 }
70
71 memset((*iLBCenc_inst).anaMem, 0, LPC_FILTERORDER * sizeof(float));
72 memcpy((*iLBCenc_inst).lsfold, lsfmeanTbl,
73 LPC_FILTERORDER * sizeof(float));
74 memcpy((*iLBCenc_inst).lsfdeqold, lsfmeanTbl,
75 LPC_FILTERORDER * sizeof(float));
76 memset((*iLBCenc_inst).lpc_buffer, 0,
77 (LPC_LOOKBACK + BLOCKL_MAX) * sizeof(float));
78 memset((*iLBCenc_inst).hpimem, 0, 4 * sizeof(float));
79
80 return (iLBCenc_inst->no_of_bytes);
81 }
82
83 /*----------------------------------------------------------------*
84 * main encoder function
85 *---------------------------------------------------------------*/
86
87 void iLBC_encode(unsigned char *bytes, /* (o) encoded data bits iLBC */
88 float *block, /* (o) speech vector to
89 encode */
90 iLBC_Enc_Inst_t * iLBCenc_inst /* (i/o) the general encoder
91 state */
92 )
93 {
94
95 float data[BLOCKL_MAX];
96 float residual[BLOCKL_MAX], reverseResidual[BLOCKL_MAX];
97
98 int start, idxForMax, idxVec[STATE_LEN];
99
100
101
102
103
104 float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
105 int n, k, meml_gotten, Nfor, Nback, i, pos;
106 int gain_index[CB_NSTAGES * NASUB_MAX], extra_gain_index[CB_NSTAGES];
107 int cb_index[CB_NSTAGES * NASUB_MAX], extra_cb_index[CB_NSTAGES];
108 int lsf_i[LSF_NSPLIT * LPC_N_MAX];
109 unsigned char *pbytes;
110 int diff, start_pos, state_first;
111 float en1, en2;
112 int index, ulp, firstpart;
113 int subcount, subframe;
114 float weightState[LPC_FILTERORDER];
115 float syntdenum[NSUB_MAX * (LPC_FILTERORDER + 1)];
116 float weightdenum[NSUB_MAX * (LPC_FILTERORDER + 1)];
117 float decresidual[BLOCKL_MAX];
118
119 /* high pass filtering of input signal if such is not done
120 prior to calling this function */
121
122 hpInput(block, iLBCenc_inst->blockl, data, (*iLBCenc_inst).hpimem);
123
124 /* otherwise simply copy */
125
126 /*memcpy(data,block,iLBCenc_inst->blockl*sizeof(float)); */
127
128 /* LPC of hp filtered input data */
129
130 LPCencode(syntdenum, weightdenum, lsf_i, data, iLBCenc_inst);
131
132
133 /* inverse filter to get residual */
134
135 for (n = 0; n < iLBCenc_inst->nsub; n++) {
136 anaFilter(&data[n * SUBL], &syntdenum[n * (LPC_FILTERORDER + 1)],
137 SUBL, &residual[n * SUBL], iLBCenc_inst->anaMem);
138 }
139
140 /* find state location */
141
142 start = FrameClassify(iLBCenc_inst, residual);
143
144 /* check if state should be in first or last part of the
145 two subframes */
146
147 diff = STATE_LEN - iLBCenc_inst->state_short_len;
148 en1 = 0;
149 index = (start - 1) * SUBL;
150
151
152
153
154
155 for (i = 0; i < iLBCenc_inst->state_short_len; i++) {
156 en1 += residual[index + i] * residual[index + i];
157 }
158 en2 = 0;
159 index = (start - 1) * SUBL + diff;
160 for (i = 0; i < iLBCenc_inst->state_short_len; i++) {
161 en2 += residual[index + i] * residual[index + i];
162 }
163
164
165 if (en1 > en2) {
166 state_first = 1;
167 start_pos = (start - 1) * SUBL;
168 } else {
169 state_first = 0;
170 start_pos = (start - 1) * SUBL + diff;
171 }
172
173 /* scalar quantization of state */
174
175 StateSearchW(iLBCenc_inst, &residual[start_pos],
176 &syntdenum[(start - 1) * (LPC_FILTERORDER + 1)],
177 &weightdenum[(start - 1) * (LPC_FILTERORDER + 1)], &idxForMax,
178 idxVec, iLBCenc_inst->state_short_len, state_first);
179
180 StateConstructW(idxForMax, idxVec,
181 &syntdenum[(start - 1) * (LPC_FILTERORDER + 1)],
182 &decresidual[start_pos], iLBCenc_inst->state_short_len);
183
184 /* predictive quantization in state */
185
186 if (state_first) { /* put adaptive part in the end */
187
188 /* setup memory */
189
190 memset(mem, 0,
191 (CB_MEML - iLBCenc_inst->state_short_len) * sizeof(float));
192 memcpy(mem + CB_MEML - iLBCenc_inst->state_short_len,
193 decresidual + start_pos,
194 iLBCenc_inst->state_short_len * sizeof(float));
195 memset(weightState, 0, LPC_FILTERORDER * sizeof(float));
196
197 /* encode sub-frames */
198
199 iCBSearch(iLBCenc_inst, extra_cb_index, extra_gain_index,
200 &residual[start_pos + iLBCenc_inst->state_short_len],
201 mem + CB_MEML - stMemLTbl,
202 stMemLTbl, diff, CB_NSTAGES,
203 &weightdenum[start * (LPC_FILTERORDER + 1)], weightState, 0);
204
205 /* construct decoded vector */
206
207 iCBConstruct(&decresidual[start_pos +
208 iLBCenc_inst->state_short_len], extra_cb_index,
209 extra_gain_index, mem + CB_MEML - stMemLTbl, stMemLTbl, diff,
210 CB_NSTAGES);
211
212 } else { /* put adaptive part in the beginning */
213
214 /* create reversed vectors for prediction */
215
216 for (k = 0; k < diff; k++) {
217 reverseResidual[k] = residual[(start + 1) * SUBL - 1
218 - (k + iLBCenc_inst->state_short_len)];
219 }
220
221 /* setup memory */
222
223 meml_gotten = iLBCenc_inst->state_short_len;
224 for (k = 0; k < meml_gotten; k++) {
225 mem[CB_MEML - 1 - k] = decresidual[start_pos + k];
226 }
227 memset(mem, 0, (CB_MEML - k) * sizeof(float));
228 memset(weightState, 0, LPC_FILTERORDER * sizeof(float));
229
230 /* encode sub-frames */
231
232 iCBSearch(iLBCenc_inst, extra_cb_index, extra_gain_index,
233 reverseResidual, mem + CB_MEML - stMemLTbl, stMemLTbl,
234 diff, CB_NSTAGES,
235 &weightdenum[(start - 1) * (LPC_FILTERORDER + 1)],
236 weightState, 0);
237
238 /* construct decoded vector */
239
240 iCBConstruct(reverseDecresidual, extra_cb_index,
241 extra_gain_index, mem + CB_MEML - stMemLTbl, stMemLTbl,
242 diff, CB_NSTAGES);
243
244 /* get decoded residual from reversed vector */
245
246 for (k = 0; k < diff; k++) {
247 decresidual[start_pos - 1 - k] = reverseDecresidual[k];
248
249
250
251
252
253 }
254 }
255
256 /* counter for predicted sub-frames */
257
258 subcount = 0;
259
260 /* forward prediction of sub-frames */
261
262 Nfor = iLBCenc_inst->nsub - start - 1;
263
264
265 if (Nfor > 0) {
266
267 /* setup memory */
268
269 memset(mem, 0, (CB_MEML - STATE_LEN) * sizeof(float));
270 memcpy(mem + CB_MEML - STATE_LEN, decresidual + (start - 1) * SUBL,
271 STATE_LEN * sizeof(float));
272 memset(weightState, 0, LPC_FILTERORDER * sizeof(float));
273
274 /* loop over sub-frames to encode */
275
276 for (subframe = 0; subframe < Nfor; subframe++) {
277
278 /* encode sub-frame */
279
280 iCBSearch(iLBCenc_inst, cb_index + subcount * CB_NSTAGES,
281 gain_index + subcount * CB_NSTAGES,
282 &residual[(start + 1 + subframe) * SUBL],
283 mem + CB_MEML - memLfTbl[subcount],
284 memLfTbl[subcount], SUBL, CB_NSTAGES,
285 &weightdenum[(start + 1 + subframe) *
286 (LPC_FILTERORDER + 1)], weightState, subcount + 1);
287
288 /* construct decoded vector */
289
290 iCBConstruct(&decresidual[(start + 1 + subframe) * SUBL],
291 cb_index + subcount * CB_NSTAGES,
292 gain_index + subcount * CB_NSTAGES,
293 mem + CB_MEML - memLfTbl[subcount],
294 memLfTbl[subcount], SUBL, CB_NSTAGES);
295
296 /* update memory */
297
298 memcpy(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(float));
299 memcpy(mem + CB_MEML - SUBL,
300 &decresidual[(start + 1 + subframe) * SUBL],
301 SUBL * sizeof(float));
302 memset(weightState, 0, LPC_FILTERORDER * sizeof(float));
303
304 subcount++;
305 }
306 }
307
308
309 /* backward prediction of sub-frames */
310
311 Nback = start - 1;
312
313
314 if (Nback > 0) {
315
316 /* create reverse order vectors */
317
318 for (n = 0; n < Nback; n++) {
319 for (k = 0; k < SUBL; k++) {
320 reverseResidual[n * SUBL + k] =
321 residual[(start - 1) * SUBL - 1 - n * SUBL - k];
322 reverseDecresidual[n * SUBL + k] =
323 decresidual[(start - 1) * SUBL - 1 - n * SUBL - k];
324 }
325 }
326
327 /* setup memory */
328
329 meml_gotten = SUBL * (iLBCenc_inst->nsub + 1 - start);
330
331
332 if (meml_gotten > CB_MEML) {
333 meml_gotten = CB_MEML;
334 }
335 for (k = 0; k < meml_gotten; k++) {
336 mem[CB_MEML - 1 - k] = decresidual[(start - 1) * SUBL + k];
337 }
338 memset(mem, 0, (CB_MEML - k) * sizeof(float));
339 memset(weightState, 0, LPC_FILTERORDER * sizeof(float));
340
341 /* loop over sub-frames to encode */
342
343 for (subframe = 0; subframe < Nback; subframe++) {
344
345 /* encode sub-frame */
346
347 iCBSearch(iLBCenc_inst, cb_index + subcount * CB_NSTAGES,
348 gain_index + subcount * CB_NSTAGES,
349 &reverseResidual[subframe * SUBL],
350 mem + CB_MEML - memLfTbl[subcount],
351 memLfTbl[subcount], SUBL, CB_NSTAGES,
352 &weightdenum[(start - 2 - subframe) *
353 (LPC_FILTERORDER + 1)], weightState, subcount + 1);
354
355 /* construct decoded vector */
356
357 iCBConstruct(&reverseDecresidual[subframe * SUBL],
358 cb_index + subcount * CB_NSTAGES,
359 gain_index + subcount * CB_NSTAGES,
360 mem + CB_MEML - memLfTbl[subcount],
361 memLfTbl[subcount], SUBL, CB_NSTAGES);
362
363 /* update memory */
364
365 memcpy(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(float));
366 memcpy(mem + CB_MEML - SUBL,
367 &reverseDecresidual[subframe * SUBL], SUBL * sizeof(float));
368 memset(weightState, 0, LPC_FILTERORDER * sizeof(float));
369
370 subcount++;
371
372 }
373
374 /* get decoded residual from reversed vector */
375
376 for (i = 0; i < SUBL * Nback; i++) {
377 decresidual[SUBL * Nback - i - 1] = reverseDecresidual[i];
378 }
379 }
380 /* end encoding part */
381
382 /* adjust index */
383 index_conv_enc(cb_index);
384
385 /* pack bytes */
386
387 pbytes = bytes;
388 pos = 0;
389
390 /* loop over the 3 ULP classes */
391
392 for (ulp = 0; ulp < 3; ulp++) {
393
394
395
396
397
398
399 /* LSF */
400 for (k = 0; k < LSF_NSPLIT * iLBCenc_inst->lpc_n; k++) {
401 packsplit(&lsf_i[k], &firstpart, &lsf_i[k],
402 iLBCenc_inst->ULP_inst->lsf_bits[k][ulp],
403 iLBCenc_inst->ULP_inst->lsf_bits[k][ulp] +
404 iLBCenc_inst->ULP_inst->lsf_bits[k][ulp + 1] +
405 iLBCenc_inst->ULP_inst->lsf_bits[k][ulp + 2]);
406 dopack(&pbytes, firstpart,
407 iLBCenc_inst->ULP_inst->lsf_bits[k][ulp], &pos);
408 }
409
410 /* Start block info */
411
412 packsplit(&start, &firstpart, &start,
413 iLBCenc_inst->ULP_inst->start_bits[ulp],
414 iLBCenc_inst->ULP_inst->start_bits[ulp] +
415 iLBCenc_inst->ULP_inst->start_bits[ulp + 1] +
416 iLBCenc_inst->ULP_inst->start_bits[ulp + 2]);
417 dopack(&pbytes, firstpart,
418 iLBCenc_inst->ULP_inst->start_bits[ulp], &pos);
419
420 packsplit(&state_first, &firstpart, &state_first,
421 iLBCenc_inst->ULP_inst->startfirst_bits[ulp],
422 iLBCenc_inst->ULP_inst->startfirst_bits[ulp] +
423 iLBCenc_inst->ULP_inst->startfirst_bits[ulp + 1] +
424 iLBCenc_inst->ULP_inst->startfirst_bits[ulp + 2]);
425 dopack(&pbytes, firstpart,
426 iLBCenc_inst->ULP_inst->startfirst_bits[ulp], &pos);
427
428 packsplit(&idxForMax, &firstpart, &idxForMax,
429 iLBCenc_inst->ULP_inst->scale_bits[ulp],
430 iLBCenc_inst->ULP_inst->scale_bits[ulp] +
431 iLBCenc_inst->ULP_inst->scale_bits[ulp + 1] +
432 iLBCenc_inst->ULP_inst->scale_bits[ulp + 2]);
433 dopack(&pbytes, firstpart,
434 iLBCenc_inst->ULP_inst->scale_bits[ulp], &pos);
435
436 for (k = 0; k < iLBCenc_inst->state_short_len; k++) {
437 packsplit(idxVec + k, &firstpart, idxVec + k,
438 iLBCenc_inst->ULP_inst->state_bits[ulp],
439 iLBCenc_inst->ULP_inst->state_bits[ulp] +
440 iLBCenc_inst->ULP_inst->state_bits[ulp + 1] +
441 iLBCenc_inst->ULP_inst->state_bits[ulp + 2]);
442 dopack(&pbytes, firstpart,
443 iLBCenc_inst->ULP_inst->state_bits[ulp], &pos);
444 }
445
446
447
448
449
450
451 /* 23/22 (20ms/30ms) sample block */
452
453 for (k = 0; k < CB_NSTAGES; k++) {
454 packsplit(extra_cb_index + k, &firstpart,
455 extra_cb_index + k,
456 iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp],
457 iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp] +
458 iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp + 1] +
459 iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp + 2]);
460 dopack(&pbytes, firstpart,
461 iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp], &pos);
462 }
463
464 for (k = 0; k < CB_NSTAGES; k++) {
465 packsplit(extra_gain_index + k, &firstpart,
466 extra_gain_index + k,
467 iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp],
468 iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp] +
469 iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp + 1] +
470 iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp + 2]);
471 dopack(&pbytes, firstpart,
472 iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp], &pos);
473 }
474
475 /* The two/four (20ms/30ms) 40 sample sub-blocks */
476
477 for (i = 0; i < iLBCenc_inst->nasub; i++) {
478 for (k = 0; k < CB_NSTAGES; k++) {
479 packsplit(cb_index + i * CB_NSTAGES + k, &firstpart,
480 cb_index + i * CB_NSTAGES + k,
481 iLBCenc_inst->ULP_inst->cb_index[i][k][ulp],
482 iLBCenc_inst->ULP_inst->cb_index[i][k][ulp] +
483 iLBCenc_inst->ULP_inst->cb_index[i][k][ulp + 1] +
484 iLBCenc_inst->ULP_inst->cb_index[i][k][ulp + 2]);
485 dopack(&pbytes, firstpart,
486 iLBCenc_inst->ULP_inst->cb_index[i][k][ulp], &pos);
487 }
488 }
489
490 for (i = 0; i < iLBCenc_inst->nasub; i++) {
491 for (k = 0; k < CB_NSTAGES; k++) {
492 packsplit(gain_index + i * CB_NSTAGES + k, &firstpart,
493 gain_index + i * CB_NSTAGES + k,
494 iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp],
495 iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp] +
496 iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp + 1] +
497 iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp + 2]);
498 dopack(&pbytes, firstpart,
499 iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp], &pos);
500 }
501 }
502 }
503
504 /* set the last bit to zero (otherwise the decoder
505 will treat it as a lost frame) */
506 dopack(&pbytes, 0, 1, &pos);
507 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.