comparison 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
comparison
equal deleted inserted replaced
241:a48ddaa0f9a7 242:f081a6850fb5
1 /* 1 /*
2 * In this module we implement our handling of BFI frame gaps. 2 * In this module we implement our handling of BFI frame gaps
3 * and invalid SID frames.
3 */ 4 */
4 5
5 #include <stdint.h> 6 #include <stdint.h>
6 #include <string.h> 7 #include <string.h>
7 #include "gsm_fr_preproc.h" 8 #include "gsm_fr_preproc.h"
36 frame[sub*7+6] &= 0x9F; 37 frame[sub*7+6] &= 0x9F;
37 frame[sub*7+6] |= Mc << 5; 38 frame[sub*7+6] |= Mc << 5;
38 } 39 }
39 } 40 }
40 41
42 static int reduce_xmaxc_sid(struct gsmfr_preproc_state *st)
43 {
44 if (st->sid_xmaxc > 4) {
45 st->sid_xmaxc -= 4;
46 return 0;
47 } else {
48 st->sid_xmaxc = 0;
49 return 1;
50 }
51 }
52
41 void gsmfr_preproc_bfi(struct gsmfr_preproc_state *st, int taf, gsm_byte *frame) 53 void gsmfr_preproc_bfi(struct gsmfr_preproc_state *st, int taf, gsm_byte *frame)
42 { 54 {
43 int mute; 55 int mute;
44 56
45 switch (st->rx_state) { 57 switch (st->rx_state) {
56 random_grid_pos(st, frame); 68 random_grid_pos(st, frame);
57 if (mute) 69 if (mute)
58 st->rx_state = NO_DATA; 70 st->rx_state = NO_DATA;
59 return; 71 return;
60 case COMFORT_NOISE: 72 case COMFORT_NOISE:
61 gsmfr_preproc_gen_cn(st, frame);
62 if (taf) 73 if (taf)
63 st->rx_state = LOST_SID; 74 st->rx_state = LOST_SID;
75 gsmfr_preproc_gen_cn(st, frame);
64 return; 76 return;
65 case LOST_SID: 77 case LOST_SID:
78 if (taf) {
79 st->rx_state = CN_MUTING;
80 reduce_xmaxc_sid(st);
81 }
66 gsmfr_preproc_gen_cn(st, frame); 82 gsmfr_preproc_gen_cn(st, frame);
67 if (taf) 83 return;
84 case CN_MUTING:
85 if (reduce_xmaxc_sid(st)) {
68 st->rx_state = NO_DATA; 86 st->rx_state = NO_DATA;
87 memcpy(frame, &gsmfr_preproc_silence_frame,
88 sizeof(gsm_frame));
89 } else
90 gsmfr_preproc_gen_cn(st, frame);
69 return; 91 return;
70 } 92 }
71 } 93 }
94
95 void gsmfr_preproc_invalid_sid(struct gsmfr_preproc_state *st, gsm_byte *frame)
96 {
97 switch (st->rx_state) {
98 case NO_DATA:
99 memcpy(frame, &gsmfr_preproc_silence_frame, sizeof(gsm_frame));
100 return;
101 case SPEECH:
102 /*
103 * Make CN out of the last good speech frame, following the
104 * "NOTE" at the end of section 6.1.2 in TS 46.031.
105 */
106 st->rx_state = COMFORT_NOISE;
107 memcpy(st->sid_prefix, &st->speech_frame, 5);
108 st->sid_xmaxc = ((st->speech_frame[27] & 0x1F) << 1) |
109 (st->speech_frame[28] >> 7);
110 gsmfr_preproc_gen_cn(st, frame);
111 return;
112 case SPEECH_MUTING:
113 /* same as above, but set the state to CN_MUTING */
114 st->rx_state = CN_MUTING;
115 memcpy(st->sid_prefix, &st->speech_frame, 5);
116 st->sid_xmaxc = ((st->speech_frame[27] & 0x1F) << 1) |
117 (st->speech_frame[28] >> 7);
118 gsmfr_preproc_gen_cn(st, frame);
119 return;
120 case COMFORT_NOISE:
121 case LOST_SID:
122 st->rx_state = COMFORT_NOISE;
123 gsmfr_preproc_gen_cn(st, frame);
124 return;
125 case CN_MUTING:
126 if (reduce_xmaxc_sid(st)) {
127 st->rx_state = NO_DATA;
128 memcpy(frame, &gsmfr_preproc_silence_frame,
129 sizeof(gsm_frame));
130 } else
131 gsmfr_preproc_gen_cn(st, frame);
132 return;
133 }
134 }