view libgsmfrp/bad_frame.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 2361a7d8c1eb
children 6ac547f0b903
line wrap: on
line source

/*
 * In this module we implement our handling of BFI frame gaps
 * and invalid SID frames.
 */

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

static int reduce_xmaxc(gsm_byte *frame)
{
	int mute_flag = 1;
	unsigned sub, xmaxc;

	for (sub = 0; sub < 4; sub++) {
		xmaxc = ((frame[sub*7+6] & 0x1F) << 1) | (frame[sub*7+7] >> 7);
		if (xmaxc > 4) {
			xmaxc -= 4;
			mute_flag = 0;
		} else
			xmaxc = 0;
		frame[sub*7+6] &= 0xE0;
		frame[sub*7+6] |= xmaxc >> 1;
		frame[sub*7+7] &= 0x7F;
		frame[sub*7+7] |= (xmaxc & 1) << 7;
	}
	return mute_flag;
}

static void random_grid_pos(struct gsmfr_preproc_state *st, gsm_byte *frame)
{
	unsigned sub, Mc;

	for (sub = 0; sub < 4; sub++) {
		Mc = gsmfr_preproc_prng(st, 2);
		frame[sub*7+6] &= 0x9F;
		frame[sub*7+6] |= Mc << 5;
	}
}

static int reduce_xmaxc_sid(struct gsmfr_preproc_state *st)
{
	if (st->sid_xmaxc > 4) {
		st->sid_xmaxc -= 4;
		return 0;
	} else {
		st->sid_xmaxc = 0;
		return 1;
	}
}

void gsmfr_preproc_bfi(struct gsmfr_preproc_state *st, int taf, gsm_byte *frame)
{
	int mute;

	switch (st->rx_state) {
	case NO_DATA:
		memcpy(frame, &gsmfr_preproc_silence_frame, sizeof(gsm_frame));
		return;
	case SPEECH:
		memcpy(frame, &st->speech_frame, sizeof(gsm_frame));
		st->rx_state = SPEECH_MUTING;
		return;
	case SPEECH_MUTING:
		mute = reduce_xmaxc(st->speech_frame);
		memcpy(frame, &st->speech_frame, sizeof(gsm_frame));
		random_grid_pos(st, frame);
		if (mute)
			st->rx_state = NO_DATA;
		return;
	case COMFORT_NOISE:
		if (taf)
			st->rx_state = LOST_SID;
		gsmfr_preproc_gen_cn(st, frame);
		return;
	case LOST_SID:
		if (taf) {
			st->rx_state = CN_MUTING;
			reduce_xmaxc_sid(st);
		}
		gsmfr_preproc_gen_cn(st, frame);
		return;
	case CN_MUTING:
		if (reduce_xmaxc_sid(st)) {
			st->rx_state = NO_DATA;
			memcpy(frame, &gsmfr_preproc_silence_frame,
				sizeof(gsm_frame));
		} else
			gsmfr_preproc_gen_cn(st, frame);
		return;
	}
}

void gsmfr_preproc_invalid_sid(struct gsmfr_preproc_state *st, gsm_byte *frame)
{
	switch (st->rx_state) {
	case NO_DATA:
		memcpy(frame, &gsmfr_preproc_silence_frame, sizeof(gsm_frame));
		return;
	case SPEECH:
		/*
		 * Make CN out of the last good speech frame, following the
		 * "NOTE" at the end of section 6.1.2 in TS 46.031.
		 */
		st->rx_state = COMFORT_NOISE;
		memcpy(st->sid_prefix, &st->speech_frame, 5);
		st->sid_xmaxc = ((st->speech_frame[27] & 0x1F) << 1) |
				(st->speech_frame[28] >> 7);
		gsmfr_preproc_gen_cn(st, frame);
		return;
	case SPEECH_MUTING:
		/* same as above, but set the state to CN_MUTING */
		st->rx_state = CN_MUTING;
		memcpy(st->sid_prefix, &st->speech_frame, 5);
		st->sid_xmaxc = ((st->speech_frame[27] & 0x1F) << 1) |
				(st->speech_frame[28] >> 7);
		gsmfr_preproc_gen_cn(st, frame);
		return;
	case COMFORT_NOISE:
	case LOST_SID:
		st->rx_state = COMFORT_NOISE;
		gsmfr_preproc_gen_cn(st, frame);
		return;
	case CN_MUTING:
		if (reduce_xmaxc_sid(st)) {
			st->rx_state = NO_DATA;
			memcpy(frame, &gsmfr_preproc_silence_frame,
				sizeof(gsm_frame));
		} else
			gsmfr_preproc_gen_cn(st, frame);
		return;
	}
}