comparison libgsmhr1/err_conc.c @ 599:762cf36e2487

libgsmhr1/err_conc.[ch]: import original
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 04 Dec 2025 10:02:09 +0000
parents
children 5a7d04bf26f5
comparison
equal deleted inserted replaced
598:5809165fb140 599:762cf36e2487
1 /***************************************************************************
2 *
3 * File Name: err_conc.c
4 *
5 * Purpose:
6 * Contains all functions for error concealment.
7 * Relevant specification: GSM 06.21
8 *
9 * Below is a listing of all the functions appearing in the file.
10 * All functions are called within speechDecoder().
11 *
12 * Error concealment on parameter level:
13 * para_conceal_speech_decoder()
14 *
15 * Error concealment on signal level:
16 * signal_conceal_sub()
17 *
18 * Additional functions to support concealment:
19 * level_estimator()
20 * level_calc()
21 *
22 **************************************************************************/
23
24 /*_________________________________________________________________________
25 | |
26 | Include Files |
27 |_________________________________________________________________________|
28 */
29
30 #include "mathhalf.h"
31 #include "sp_dec.h"
32 #include "err_conc.h"
33
34 /*_________________________________________________________________________
35 | |
36 | Local Defines |
37 |_________________________________________________________________________|
38 */
39 #define MIN_MUTE_LEVEL -45
40
41 /*_________________________________________________________________________
42 | |
43 | State variables (globals) |
44 |_________________________________________________________________________|
45 */
46
47 Longword plSubfrEnergyMem[4];
48 Shortword swLevelMem[4],
49 lastR0,
50 pswLastGood[18],
51 swState,
52 swLastFlag;
53
54
55 /*****************************************************************************
56 *
57 * FUNCTION NAME: para_conceal_speech_decoder
58 *
59 * This subroutine performs concealment on parameter level. If the
60 * badframe flag (swErrorFlag[0]) has been set in the channel decoder
61 * parameter repetition is performed.
62 * If the average frame energy R0 shows an abnormal increase between two
63 * subsequent frames the badframe flag is also set and parameter
64 * repetition is performed.
65 * If R0 shows an important increase muting is permitted in the signal
66 * concealment unit. There the level of the synthesized speech signal is
67 * controlled and corrected if necessary.
68 *
69 * In table "psrR0RepeatThreshold[]" the maximum allowed
70 * increase of R0 for badframe setting is stored. The table
71 * is controlled by the value of R0 of the last frame.
72 * If e.g. the previous R0 is 10 the allowed maximum increase
73 * is 9 (psrR0RepeatThreshold[10]).
74 * The figures in psrR0RepeatThreshold[] have been determined
75 * by measuring the R0 statistics of an error free speech
76 * signal. In approximately 95 % of the frames the increase of
77 * R0 is less than the defined figures for error free speech.
78 * If the level increase is higher than the determined limit
79 * then the badframe flag is set.
80 *
81 * In table "psrR0MuteThreshold[]" the maximum allowed
82 * increase of R0 for muting is stored.
83 * The table is controlled by the value of R0 of the last frame
84 * If e.g. the previous R0 is 10 the allowed maximum increase
85 * is 7 (psrR0MuteThreshold[10]).
86 * The figures in psrR0MuteThreshold[] have been determined
87 * by measuring the R0 statistics of an error free speech
88 * signal. In approximately 85 % of the frames the increase of
89 * R0 is less than the defined figures for error free speech.
90 * If the level increase is higher than the determined limit
91 * then muting is allowed.
92 *
93 * Input: pswErrorFlag[0] badframe flag from channel decoder
94 * pswErrorFlag[1] unreliable frame flag from channel decoder
95 * pswSpeechPara[] unconcealed speech parameters
96 * Output: pswSpeechPara[] concealed speech parameters
97 * swMutePermit flag, indicating whether muting is
98 * permitted
99 *
100 * Constants: psrR0RepeatThreshold[32] maximum allowed R0 difference
101 * before frame is repeated
102 * psrR0MuteThreshold[32] maximum allowed R0 difference
103 * before muting is permitted
104 *
105 *
106 ****************************************************************************/
107
108 void para_conceal_speech_decoder(Shortword pswErrorFlag[],
109 Shortword pswSpeechPara[], Shortword *pswMutePermit)
110 {
111
112 /*_________________________________________________________________________
113 | |
114 | Local Static Variables |
115 |_________________________________________________________________________|
116 */
117 static const Shortword psrR0RepeatThreshold[32] =
118 {15, 15, 15, 12, 12, 12, 12, 11,
119 10, 10, 9, 9, 9, 9, 8, 8,
120 7, 6, 5, 5, 5, 4, 4, 3,
121 2, 2, 2, 2, 2, 2, 10, 10};
122 static const Shortword psrR0MuteThreshold[32] =
123 {14, 12, 11, 9, 9, 9, 9, 7,
124 7, 7, 7, 7, 6, 6, 6, 5,
125 5, 4, 3, 3, 3, 3, 3, 2,
126 1, 1, 1, 1, 1, 1, 10, 10};
127
128 /*_________________________________________________________________________
129 | |
130 | Automatic Variables |
131 |_________________________________________________________________________|
132 */
133 Shortword swLastLag,
134 swR0,
135 swLag,
136 r0_diff,
137 i;
138
139
140 /*_________________________________________________________________________
141 | |
142 | Executable Code |
143 |_________________________________________________________________________|
144 */
145
146 /* Initialise mute permission flag */
147 /* ------------------------------- */
148 *pswMutePermit = 0;
149
150 /* Determine R0-difference to last frame */
151 /* ------------------------------------- */
152 r0_diff = sub(pswSpeechPara[0], lastR0);
153
154 /* If no badframe has been declared, but the frame is unreliable then */
155 /* check whether there is an abnormal increase of R0 */
156 /* ------------------------------------------------------------------ */
157 if ((pswErrorFlag[0] == 0) && (pswErrorFlag[1] > 0))
158 {
159
160 /* Check if difference exceeds the maximum allowed threshold. */
161 /* If yes, set badframe flag */
162 /* ---------------------------------------------------------- */
163 if (sub(r0_diff, psrR0RepeatThreshold[lastR0]) >= 0)
164 {
165 pswErrorFlag[0] = 1;
166 }
167 else
168 {
169 /* Allow muting if R0 >= 30 */
170 /* ------------------------ */
171 if (sub(pswSpeechPara[0], 30) >= 0)
172 *pswMutePermit = 1;
173 }
174 }
175
176 /* If no badframe has been declared, but the frame is unreliable then */
177 /* check whether there is an important increase of R0 */
178 /* ------------------------------------------------------------------ */
179 if ((pswErrorFlag[1] > 0) && (pswErrorFlag[0] == 0))
180 {
181
182 /* Check if difference exceeds a threshold. */
183 /* If yes, allow muting in the signal concealment unit */
184 /* ---------------------------------------------------------- */
185 if (sub(r0_diff, psrR0MuteThreshold[lastR0]) >= 0)
186 {
187 *pswMutePermit = 1;
188 }
189 }
190
191
192 /* Perform parameter repetition, if necessary (badframe handling) */
193 /* -------------------------------------------------------------- */
194
195 if (pswErrorFlag[0] > 0)
196 {
197 swState = add(swState, 1); /* update the bad frame
198 * masking state */
199 if (sub(swState, 6) > 0)
200 swState = 6;
201 }
202 else
203 {
204 if (sub(swState, 6) < 0)
205 swState = 0;
206 else if (swLastFlag == 0)
207 swState = 0;
208 }
209
210 swLastFlag = pswErrorFlag[0];
211
212 /* if the decoded frame is good, save it */
213 /* ------------------------------------- */
214 if (swState == 0)
215 {
216 for (i = 0; i < 18; i++)
217 pswLastGood[i] = pswSpeechPara[i];
218 }
219
220 /* if the frame is bad, attenuate and repeat last good frame */
221 /* --------------------------------------------------------- */
222 else
223 {
224 if ((sub(swState, 3) >= 0) && (sub(swState, 5) <= 0))
225 {
226 swR0 = sub(pswLastGood[0], 2); /* attenuate by 4 dB */
227 if (swR0 < 0)
228 swR0 = 0;
229 pswLastGood[0] = swR0;
230 }
231
232 if (sub(swState, 6) >= 0) /* mute */
233 pswLastGood[0] = 0;
234
235 /* If the last good frame is unvoiced, use its energy, voicing mode, lpc
236 * coefficients, and soft interpolation. For gsp0, use only the gsp0
237 * value from the last good subframe. If the current bad frame is
238 * unvoiced, use the current codewords. If not, use the codewords from
239 * the last good frame. */
240 /* -------------------------------------------------------------- */
241 if (pswLastGood[5] == 0)
242 { /* unvoiced good frame */
243 if (pswSpeechPara[5] == 0)
244 { /* unvoiced bad frame */
245 for (i = 0; i < 5; i++)
246 pswSpeechPara[i] = pswLastGood[i];
247 for (i = 0; i < 4; i++)
248 pswSpeechPara[3 * i + 8] = pswLastGood[17];
249 }
250 else
251 { /* voiced bad frame */
252 for (i = 0; i < 18; i++)
253 pswSpeechPara[i] = pswLastGood[i];
254 for (i = 0; i < 3; i++)
255 pswSpeechPara[3 * i + 8] = pswLastGood[17];
256 }
257 }
258
259 /* If the last good frame is voiced, the long term predictor lag at the
260 * last subframe is used for all subsequent subframes. Use the last good
261 * frame's energy, voicing mode, lpc coefficients, and soft
262 * interpolation. For gsp0 in all subframes, use the gsp0 value from the
263 * last good subframe. If the current bad frame is voiced, use the
264 * current codewords. If not, use the codewords from the last good
265 * frame. */
266 /* ---------------------------------------------------------------- */
267 else
268 { /* voiced good frame */
269 swLastLag = pswLastGood[6]; /* frame lag */
270 for (i = 0; i < 3; i++)
271 { /* each delta lag */
272 swLag = sub(pswLastGood[3 * i + 9], 0x8); /* biased around 0 */
273 swLag = add(swLag, swLastLag); /* reconstruct pitch */
274 if (sub(swLag, 0x00ff) > 0)
275 { /* limit, as needed */
276 swLastLag = 0x00ff;
277 }
278 else if (swLag < 0)
279 {
280 swLastLag = 0;
281 }
282 else
283 swLastLag = swLag;
284 }
285 pswLastGood[6] = swLastLag; /* saved frame lag */
286 pswLastGood[9] = 0x8; /* saved delta lags */
287 pswLastGood[12] = 0x8;
288 pswLastGood[15] = 0x8;
289
290 if (pswSpeechPara[5] != 0)
291 { /* voiced bad frame */
292 for (i = 0; i < 6; i++)
293 pswSpeechPara[i] = pswLastGood[i];
294 for (i = 0; i < 4; i++)
295 pswSpeechPara[3 * i + 6] = pswLastGood[3 * i + 6];
296 for (i = 0; i < 4; i++)
297 pswSpeechPara[3 * i + 8] = pswLastGood[17];
298 }
299 else
300 { /* unvoiced bad frame */
301 for (i = 0; i < 18; i++)
302 pswSpeechPara[i] = pswLastGood[i];
303 for (i = 0; i < 3; i++)
304 pswSpeechPara[3 * i + 8] = pswLastGood[17];
305 }
306 }
307
308 } /* end of bad frame */
309
310
311 /* Update last value of R0 */
312 /* ----------------------- */
313 lastR0 = pswSpeechPara[0];
314
315 }
316
317
318 /****************************************************************************
319 *
320 * FUNCTION NAME: level_estimator
321 *
322 * This subroutine determines the mean level and the maximum level
323 * of the last four speech sub-frames. These parameters are the basis
324 * for the level estimation in signal_conceal_sub().
325 *
326 * Input: swUpdate = 0: the levels are determined
327 * = 1: the memory of the level estimator
328 * is updated
329 * pswDecodedSpeechFrame[] synthesized speech signal
330 *
331 * Output: swLevelMean mean level of the last 4 sub-frames
332 * swLevelMax maximum level of the last 4 sub-frames
333 *
334 ***************************************************************************/
335
336 void level_estimator(Shortword update, Shortword *pswLevelMean,
337 Shortword *pswLevelMax,
338 Shortword pswDecodedSpeechFrame[])
339 {
340
341 /*_________________________________________________________________________
342 | |
343 | Automatic Variables |
344 |_________________________________________________________________________|
345 */
346 Shortword i,
347 tmp,
348 swLevelSub;
349 Longword L_sum;
350
351 /*_________________________________________________________________________
352 | |
353 | Executable Code |
354 |_________________________________________________________________________|
355 */
356
357 if (update == 0)
358 {
359
360 /* Determine mean level of the last 4 sub-frames: */
361 /* ---------------------------------------------- */
362 for (i = 0, L_sum = 0; i < 4; ++i)
363 {
364 L_sum = L_add(L_sum, plSubfrEnergyMem[i]);
365 }
366 *pswLevelMean = level_calc(1, &L_sum);
367
368 /* Determine maximum level of the last 4 sub-frames: */
369 /* ------------------------------------------------- */
370 *pswLevelMax = -72;
371 for (i = 0; i < 4; ++i)
372 {
373 if (sub(swLevelMem[i], *pswLevelMax) > 0)
374 *pswLevelMax = swLevelMem[i];
375 }
376
377 }
378 else
379 {
380 /* Determine the energy of the synthesized speech signal: */
381 /* ------------------------------------------------------ */
382 for (i = 0, L_sum = 0; i < S_LEN; ++i)
383 {
384 tmp = shr(pswDecodedSpeechFrame[i], 3);
385 L_sum = L_mac(L_sum, tmp, tmp);
386 }
387 swLevelSub = level_calc(0, &L_sum);
388
389 /* Update memories of level estimator: */
390 /* ----------------------------------- */
391 for (i = 0; i < 3; ++i)
392 plSubfrEnergyMem[i] = plSubfrEnergyMem[i + 1];
393 plSubfrEnergyMem[3] = L_sum;
394
395 for (i = 0; i < 3; ++i)
396 swLevelMem[i] = swLevelMem[i + 1];
397 swLevelMem[3] = swLevelSub;
398 }
399 }
400
401
402 /*****************************************************************************
403 *
404 * FUNCTION NAME: signal_conceal_sub
405 *
406 * This subroutine performs concealment on subframe signal level.
407 * A test synthesis is performed and the level of the synthesized speech
408 * signal is compared to the estimated level. Depending on the control
409 * flag "swMutePermit" a muting factor is determined.
410 * If muting is permitted (swMutePermit=1) and the actual sub-frame level
411 * exceeds the maximum level of the last four sub-frames "swLevelMax" plus
412 * an allowed increase "psrLevelMaxIncrease[]" then the synthesized speech
413 * signal together with the signal memories is muted.
414 * In table "psrLevelMaxIncrease[]" the maximum allowed increase
415 * of the maximum sub-frame level is stored. The table is controled by the
416 * mean level "swMeanLevel".
417 * If e.g. the level is in the range between -30 and -35 db
418 * the allowed maximum increase is 4 db (psrLevelMaxIncrease[6]).
419 * The figures in psrLevelMaxIncrease[] have been determined
420 * by measuring the level statistics of error free synthesized speech.
421 *
422 * Input: pswPPFExcit[] excitation signal
423 * pswSynthFiltState[] state of LPC synthesis filter
424 * ppswSynthAs[] LPC coefficients
425 * pswLtpStateOut[] state of long term predictor
426 * pswPPreState[] state of pitch prefilter
427 * swLevelMean mean level
428 * swLevelMax maximum level
429 * swUFI unreliable frame flag
430 * swMuteFlagOld last muting flag
431 * pswMuteFlag actual muting flag
432 * swMutePermit mute permission
433 *
434 * Output: pswPPFExcit[] muted excitation signal
435 * pswSynthFiltState[] muted state of LPC synthesis filter
436 * pswLtpStateOut[] muted state of long term predictor
437 * pswPPreState[] muted state of pitch prefilter
438 *
439 * Constants: psrConceal[0:15] muting factors
440 * psrLevelMaxIncrease[0:7] maximum allowed level increase
441 *
442 *
443 ****************************************************************************/
444
445 void signal_conceal_sub(Shortword pswPPFExcit[],
446 Shortword ppswSynthAs[], Shortword pswSynthFiltState[],
447 Shortword pswLtpStateOut[], Shortword pswPPreState[],
448 Shortword swLevelMean, Shortword swLevelMax,
449 Shortword swUFI, Shortword swMuteFlagOld,
450 Shortword *pswMuteFlag, Shortword swMutePermit)
451 {
452
453 /*_________________________________________________________________________
454 | |
455 | Local Static Variables |
456 |_________________________________________________________________________|
457 */
458 static const Shortword psrConceal[15] = {29205, 27571, 24573, 21900,
459 19519, 17396, 15504, 13818, 12315, 10976, 9783, 8719, 7771, 6925, 6172};
460 static const Shortword psrLevelMaxIncrease[16] =
461 {0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16};
462
463 /*_________________________________________________________________________
464 | |
465 | Automatic Variables |
466 |_________________________________________________________________________|
467 */
468 Shortword swMute,
469 swLevelSub,
470 i,
471 swIndex;
472 Shortword swTmp,
473 pswStateTmp[10],
474 swOutTmp[40],
475 swPermitMuteSub;
476 Longword L_sum;
477
478
479 /*_________________________________________________________________________
480 | |
481 | Executable Code |
482 |_________________________________________________________________________|
483 */
484
485 /* Test synthesis filter: */
486 /* ---------------------- */
487 for (i = 0; i < 10; ++i)
488 pswStateTmp[i] = pswSynthFiltState[i];
489
490 lpcIir(pswPPFExcit, ppswSynthAs, pswStateTmp, swOutTmp);
491
492
493 /* Determine level in db of synthesized signal: */
494 /* -------------------------------------------- */
495 L_sum = 0;
496 for (i = 0; i < S_LEN; ++i)
497 {
498 swTmp = shr(swOutTmp[i], 2);
499 L_sum = L_mac(L_sum, swTmp, swTmp);
500 }
501 swLevelSub = level_calc(0, &L_sum);
502
503
504 /* Determine index to table, specifying the allowed level increase: */
505 /* level [ 0 .. -5] --> swIndex = 0 */
506 /* level [-5 .. -10] --> swIndex = 1 etc. */
507 /*---------------------------------------------*/
508 swIndex = mult(negate(swLevelMean), 1638);
509 if (sub(swIndex, 15) > 0)
510 swIndex = 15;
511
512 /* Muting is permitted, if it is signalled from the parameter concealment */
513 /* unit or if muting has been performed in the last frame */
514 /*-----------------------------------------------------------------------*/
515 swPermitMuteSub = swMutePermit;
516 if (swMuteFlagOld > 0)
517 swPermitMuteSub = 1;
518
519 if (swPermitMuteSub > 0)
520 {
521 /* Muting is not permitted if the sub-frame level is less than */
522 /* MIN_MUTE_LEVEL */
523 /* ------------------------------------------------------------ */
524 if (sub(swLevelSub, MIN_MUTE_LEVEL) <= 0)
525 swPermitMuteSub = 0;
526
527 /* Muting is not permitted if the sub-frame level is less than */
528 /* the maximum level of the last 4 sub-frames plus the allowed */
529 /* increase */
530 /* ------------------------------------------------------------ */
531 swMute = sub(swLevelSub, add(swLevelMax, psrLevelMaxIncrease[swIndex]));
532 if (swMute <= 0)
533 swPermitMuteSub = 0;
534 }
535
536
537 /* Perform muting, if allowed */
538 /* -------------------------- */
539 if (swPermitMuteSub > 0)
540 {
541
542 if (sub(swMute, (Shortword) 15) > 0)
543 swMute = 15;
544
545 /* Keep information that muting occured for next frame */
546 /* --------------------------------------------------- */
547 if (swUFI > 0)
548 *pswMuteFlag = 1;
549
550
551 /* Mute excitation signal: */
552 /* ----------------------- */
553 for (i = 0; i < 10; ++i)
554 pswSynthFiltState[i] =
555 mult_r(pswSynthFiltState[i], psrConceal[swMute - 1]);
556 for (i = 0; i < S_LEN; ++i)
557 pswPPFExcit[i] = mult_r(pswPPFExcit[i], psrConceal[swMute - 1]);
558
559 /* Mute pitch memory: */
560 /* ------------------ */
561 for (i = 0; i < S_LEN; ++i)
562 pswLtpStateOut[i] =
563 mult_r(pswLtpStateOut[i], psrConceal[swMute - 1]);
564
565
566 /* Mute pitch prefilter memory: */
567 /* ---------------------------- */
568 for (i = 0; i < S_LEN; ++i)
569 pswPPreState[i] = mult_r(pswPPreState[i], psrConceal[swMute - 1]);
570 }
571 }
572
573
574 /****************************************************************************
575 *
576 * FUNCTION NAME: level_calc
577 *
578 * This subroutine calculates the level (db) from the energy
579 * of a speech sub-frame (swInd=0) or a speech frame (swInd=1):
580 * The level of a speech subframe is:
581 * swLevel = 10 * lg(EN/(40.*4096*4096))
582 * = 3 * ld(EN) - 88.27
583 * = (3*4*ld(EN) - 353)/4
584 * = (3*(4*POS(MSB(EN)) + 2*BIT(MSB-1) + BIT(MSB-2)) - 353)/4
585 *
586 * Input: pl_en energy of the speech subframe or frame
587 * The energy is multiplied by 2 because of the
588 * MAC routines !!
589 * swInd = 0: EN is the energy of one subframe
590 * = 1: EN is the energy of one frame
591 *
592 * Output: swLevel level in db
593 *
594 *
595 ***************************************************************************/
596
597 Shortword level_calc(Shortword swInd, Longword *pl_en)
598 {
599
600 /*_________________________________________________________________________
601 | |
602 | Automatic Variables |
603 |_________________________________________________________________________|
604 */
605 Shortword swPos,
606 swLevel;
607 Longword L_tmp;
608
609 /*_________________________________________________________________________
610 | |
611 | Executable Code |
612 |_________________________________________________________________________|
613 */
614
615 if (*pl_en != 0)
616 swPos = sub((Shortword) 29, norm_l(*pl_en));
617 else
618 swPos = 0;
619
620 /* Determine the term: 4*POS(MSB(EN)): */
621 /* ----------------------------------- */
622 swLevel = shl(swPos, 2);
623
624 /* Determine the term: 2*BIT(MSB-1): */
625 /* --------------------------------- */
626 if (swPos >= 0)
627 {
628 L_tmp = L_shl((Longword) 1, swPos);
629 if ((*pl_en & L_tmp) != 0)
630 swLevel += 2;
631 }
632
633 /* Determine the term: BIT(MSB-2): */
634 /* ------------------------------- */
635 if (--swPos >= 0)
636 {
637 L_tmp = L_shl((Longword) 1, swPos);
638 if ((*pl_en & L_tmp) != 0)
639 ++swLevel;
640 }
641
642 /* Multiply by 3: */
643 /* -------------- */
644 swLevel += shl(swLevel, 1);
645 swLevel -= (swInd == 0) ? 353 : 377;
646 swLevel = mult_r(swLevel, 0X2000); /* >> 2 */
647
648 if (sub(swLevel, -72) < 0)
649 {
650 swLevel = -72;
651 *pl_en = (swInd == 0) ? 80 : 320;
652 }
653
654 return (swLevel);
655 }