view dev/s2u-regen.c @ 581:e2d5cad04cbf

libgsmhr1 RxFE: store CN R0+LPC separately from speech In the original GSM 06.06 code the ECU for speech mode is entirely separate from the CN generator, maintaining separate state. (The main intertie between them is the speech vs CN state variable, distinguishing between speech and CN BFIs, in addition to the CN-specific function of distinguishing between initial and update SIDs.) In the present RxFE implementation I initially thought that we could use the same saved_frame buffer for both ECU and CN, overwriting just the first 4 params (R0 and LPC) when a valid SID comes in. However, I now realize it was a bad idea: the original code has a corner case (long sequence of speech-mode BFIs to put the ECU in state 6, then SID and CN-mode BFIs, then a good speech frame) that would be broken by that buffer reuse approach. We could eliminate this corner case by resetting the ECU state when passing through a CN insertion period, but doing so would needlessly increase the behavioral diffs between GSM 06.06 and our version. Solution: use a separate CN-specific buffer for CN R0+LPC parameters, and match the behavior of GSM 06.06 code in this regard.
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 13 Feb 2025 10:02:45 +0000
parents a5200ad12d58
children
line wrap: on
line source

/*
 * This program generates a G.711 mu-law encoding table of the same form
 * as the s2u[] table in the toast_ulaw.c module in libgsm/toast, i.e.,
 * an encoding table that takes only the upper 13 bits of the linear PCM
 * sample to be encoded, even though canonical mu-law encoding requires
 * 14-bit input.  The 13-bit table constructed by this program is computed
 * as if the lsb of the "proper" 14-bit input is always zero, like it is
 * expected to be when the linear PCM samples came from the output of a
 * GSM speech decoder.
 *
 * The "engine" function that does the computation is based on ulaw_compress()
 * from ITU-T G.191 STL.
 */

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

static unsigned
ulaw_compress(input)
    unsigned input;
{
    short i;                    /* aux.var. */
    short absno;                /* absolute value of linear (input) sample */
    short segno;                /* segment (Table 2/G711, column 1) */
    short low_nibble;           /* low nibble of log companded sample */
    short high_nibble;          /* high nibble of log companded sample */
    unsigned output;

    /* -------------------------------------------------------------------- */
    /* Input is 14-bit right-justified in this version */
    /* Compute absolute value; adjust for easy processing */
    /* -------------------------------------------------------------------- */
    absno = input >= 0x2000     /* compute 1's complement in case of */
      ? (~input & 0x1FFF) + 33          /* negative samples */
      : input + 33;                     /* NB: 33 is the difference value */
    /* between the thresholds for */
    /* A-law and u-law. */
    if (absno > (0x1FFF))       /* limitation to "absno" < 8192 */
      absno = (0x1FFF);

    /* Determination of sample's segment */
    i = absno >> 6;
    segno = 1;
    while (i != 0) {
      segno++;
      i >>= 1;
    }

    /* Mounting the high-nibble of the log-PCM sample */
    high_nibble = (0x0008) - segno;

    /* Mounting the low-nibble of the log PCM sample */
    low_nibble = (absno >> segno)       /* right shift of mantissa and */
      &(0x000F);                /* masking away leading '1' */
    low_nibble = (0x000F) - low_nibble;

    /* Joining the high-nibble and the low-nibble of the log PCM sample */
    output = (high_nibble << 4) | low_nibble;

    /* Add sign bit */
    if (input < 0x2000)
      output = output | (0x0080);

    return output;
}

main(argc, argv)
	char **argv;
{
	unsigned input, output;

	for (input = 0; input < 8192; input++) {
		output = ulaw_compress(input << 1);
		printf("%04o,", output);
		if ((input % 15) == 14 || input == 8191)
			putchar('\n');
	}
	exit(0);
}