view libgsmfrp/comfort_noise.c @ 242:f081a6850fb5

libgsmfrp: new refined implementation The previous implementation exhibited the following defects, which are now fixed: 1) The last received valid SID was cached forever for the purpose of handling future invalid SIDs - we could have received some valid SID ages ago, then lots of speech or NO_DATA, and if we then get an invalid SID, we would resurrect the last valid SID from ancient history - a bad design. In our new design, we handle invalid SID based on the current state, much like BFI. 2) GSM 06.11 spec says clearly that after the second lost SID (received BFI=1 && TAF=1 in CN state) we need to gradually decrease the output level, rather than jump directly to emitting silence frames - we previously failed to implement such logic. 3) Per GSM 06.12 section 5.2, Xmaxc should be the same in all 4 subframes in a SID frame. What should we do if we receive an otherwise valid SID frame with different Xmaxc? Our previous approach would replicate this Xmaxc oddity in every subsequent generated CN frame, which is rather bad. In our new design, the very first CN frame (which can be seen as a transformation of the SID frame itself) retains the original 4 distinct Xmaxc, but all subsequent CN frames are based on the Xmaxc from the last subframe of the most recent SID.
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 09 May 2023 05:16:31 +0000
parents 3b64f255689a
children
line wrap: on
line source

/*
 * In this module we implement comfort noise generation per GSM 06.12
 * or 3GPP TS 46.012.
 */

#include <stdint.h>
#include <string.h>
#include "gsm_fr_preproc.h"
#include "internal.h"

static const uint8_t fold_table_8to6[24] = {
	1, 2, 3, 4, 5, 6, 1, 2,
	1, 2, 3, 4, 5, 6, 3, 4,
	1, 2, 3, 4, 5, 6, 5, 6,
};

static const uint8_t bc[4] = {0, 0, 0, 0};
static const uint8_t Nc[4] = {40, 120, 40, 120};

static uint8_t random_1to6(struct gsmfr_preproc_state *st)
{
	uint8_t range8, range6;

	range8 = gsmfr_preproc_prng(st, 3);
	range6 = fold_table_8to6[(st->cn_random_6fold << 3) | range8];
	st->cn_random_6fold++;
	if (st->cn_random_6fold >= 3)
		st->cn_random_6fold = 0;
	return range6;
}

void gsmfr_preproc_gen_cn(struct gsmfr_preproc_state *st, gsm_byte *frame)
{
	unsigned sub, pulse;
	uint8_t Mc, xmc[13];
	gsm_byte *c;

	/* global bytes (magic and LARc) are fixed */
	memcpy(frame, st->sid_prefix, 5);
	c = frame + 5;
	/* now do the 4 subframes, mostly PRNG output */
	for (sub = 0; sub < 4; sub++) {
		Mc = gsmfr_preproc_prng(st, 2);
		for (pulse = 0; pulse < 13; pulse++)
			xmc[pulse] = random_1to6(st);
		/* packing code from libgsm */
		*c++ =   ((Nc[sub] & 0x7F) << 1)
		       | ((bc[sub] >> 1) & 0x1);
		*c++ =   ((bc[sub] & 0x1) << 7)
		       | ((Mc & 0x3) << 5)
		       | ((st->sid_xmaxc >> 1) & 0x1F);
		*c++ =   ((st->sid_xmaxc & 0x1) << 7)
		       | ((xmc[0] & 0x7) << 4)
		       | ((xmc[1] & 0x7) << 1)
		       | ((xmc[2] >> 2) & 0x1);
		*c++ =   ((xmc[2] & 0x3) << 6)
		       | ((xmc[3] & 0x7) << 3)
		       | (xmc[4] & 0x7);
		*c++ =   ((xmc[5] & 0x7) << 5)
		       | ((xmc[6] & 0x7) << 2)
		       | ((xmc[7] >> 1) & 0x3);
		*c++ =   ((xmc[7] & 0x1) << 7)
		       | ((xmc[8] & 0x7) << 4)
		       | ((xmc[9] & 0x7) << 1)
		       | ((xmc[10] >> 2) & 0x1);
		*c++ =   ((xmc[10] & 0x3) << 6)
		       | ((xmc[11] & 0x7) << 3)
		       | (xmc[12] & 0x7);
	}
}

void gsmfr_preproc_sid2cn(struct gsmfr_preproc_state *st, gsm_byte *frame)
{
	unsigned sub, pulse;
	uint8_t Mc, xmc[13];
	gsm_byte *c;

	/* save LARc and Xmaxc from the last subframe for subsequent CN gen */
	memcpy(st->sid_prefix, frame, 5);
	st->sid_xmaxc = ((frame[27] & 0x1F) << 1) | (frame[28] >> 7);
	/* ... and turn *this* frame into very first CN output */
	c = frame + 5;
	for (sub = 0; sub < 4; sub++) {
		Mc = gsmfr_preproc_prng(st, 2);
		for (pulse = 0; pulse < 13; pulse++)
			xmc[pulse] = random_1to6(st);
		/* keep each of Xmaxc and replace the rest with CN */
		*c++ =   ((Nc[sub] & 0x7F) << 1)
		       | ((bc[sub] >> 1) & 0x1);
		*c &= 0x1F;
		*c++ |=  ((bc[sub] & 0x1) << 7)
		       | ((Mc & 0x3) << 5);
		*c &= 0x80;
		*c++ |=  ((xmc[0] & 0x7) << 4)
		       | ((xmc[1] & 0x7) << 1)
		       | ((xmc[2] >> 2) & 0x1);
		*c++ =   ((xmc[2] & 0x3) << 6)
		       | ((xmc[3] & 0x7) << 3)
		       | (xmc[4] & 0x7);
		*c++ =   ((xmc[5] & 0x7) << 5)
		       | ((xmc[6] & 0x7) << 2)
		       | ((xmc[7] >> 1) & 0x3);
		*c++ =   ((xmc[7] & 0x1) << 7)
		       | ((xmc[8] & 0x7) << 4)
		       | ((xmc[9] & 0x7) << 1)
		       | ((xmc[10] >> 2) & 0x1);
		*c++ =   ((xmc[10] & 0x3) << 6)
		       | ((xmc[11] & 0x7) << 3)
		       | (xmc[12] & 0x7);
	}
}