~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

« back to all changes in this revision

Viewing changes to daemon/src/audio/codecs/g722.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2012-05-19 21:46:37 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20120519214637-la8rbrford5kj6m3
Tags: 1.1.0-1
* New upstream release 
  - Fixes "FTBFS with libccrtp-dev/2.0.2 from experimental" (Closes: #663282)
* NEW Maintainer: Debian VoIP Team - Thanks Francois for your work.
  - (Closes: #665789: O: sflphone -- SIP and IAX2 compatible VoIP phone)
* Added Build-Depends: libdbus-c++-bin
* Add gcc47-fixes.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 *  as that of the covered work.
31
31
 */
32
32
 
33
 
 
34
 
 
35
 
#include "global.h"
36
 
#include "../common.h"
37
33
#include "audiocodec.h"
 
34
#include "sfl_types.h"
38
35
#include "g722.h"
39
 
#include "noncopyable.h"
40
36
 
41
 
#include <stdlib.h>
42
 
#include <string.h>
43
 
#include <cassert>
 
37
#include <cstdlib>
 
38
#include <cstring>
44
39
 
45
40
class G722 : public sfl::AudioCodec {
46
41
 
47
42
    public:
48
 
        G722(int payload=9) : sfl::AudioCodec(payload, "G722"),
49
 
        decode_s(new g722_decode_state_t),
50
 
        encode_s(new g722_encode_state_t) {
51
 
            clockRate_ = 16000;
52
 
            frameSize_ = 320; // samples, 20 ms at 16kHz
53
 
            channel_   = 1;
 
43
        G722() : sfl::AudioCodec(9, "G722", 16000, 320, 1), decode_state_(),
 
44
        encode_state_() {
54
45
            bitrate_ = 64;
55
46
            hasDynamicPayload_ = false;
56
47
 
57
 
            g722_decode_init();
58
 
            g722_encode_init();
59
 
        }
60
 
 
61
 
        ~G722() {
62
 
            g722_decode_release();
63
 
            g722_encode_release();
64
 
        }
65
 
 
66
 
        virtual int decode(short *dst, unsigned char *src, size_t buf_size) {
67
 
            assert(buf_size == frameSize_ / sizeof(SFLDataFormat) * encode_s->bits_per_sample / 8);
68
 
            return g722_decode((int16_t*) dst, (const uint8_t*) src, buf_size);
69
 
        }
70
 
 
71
 
        virtual int encode(unsigned char *dst, short *src, size_t buf_size) {
72
 
            int out = g722_encode((uint8_t*) dst, (const int16_t*) src, frameSize_);
73
 
            assert((size_t)out <= buf_size);
 
48
            g722_state_init(decode_state_);
 
49
            g722_state_init(encode_state_);
 
50
        }
 
51
 
 
52
    private:
 
53
        virtual int decode(SFLDataFormat *dst, unsigned char *src, size_t buf_size)
 
54
        {
 
55
            return g722_decode(dst, src, buf_size);
 
56
        }
 
57
 
 
58
        virtual int encode(unsigned char *dst, SFLDataFormat *src, size_t /*buf_size*/)
 
59
        {
 
60
            int out = g722_encode(dst, src, frameSize_);
74
61
            return out;
75
62
        }
76
63
 
77
 
 
78
 
        void g722_encode_init() {
79
 
            encode_s->itu_test_mode = false;
80
 
 
81
 
            // 8 => 64 kbps;  7 => 56 kbps;  6 => 48 kbps
82
 
            encode_s->bits_per_sample = 8;
83
 
 
84
 
            // Enable 8khz mode, encode using lower subband only
85
 
            encode_s->eight_k = false;
86
 
 
87
 
            // Never set packed true when using 64 kbps
88
 
            encode_s->packed = false;
89
 
 
90
 
            memset(encode_s->band, 0, sizeof(decode_s->band));
91
 
            encode_s->band[0].det = 32;
92
 
            encode_s->band[1].det = 8;
93
 
 
94
 
            memset(encode_s->x, 0, sizeof(encode_s->x));
95
 
 
96
 
            decode_s->in_buffer = 0;
97
 
            decode_s->in_bits = 0;
98
 
            decode_s->out_buffer = 0;
99
 
            decode_s->out_bits = 0;
100
 
        }
101
 
 
102
 
        void g722_decode_init() {
103
 
 
104
 
            decode_s->itu_test_mode = false;
105
 
 
106
 
            // 8 => 64 kbps;  7 => 56 kbps;  6 => 48 kbps
107
 
            decode_s->bits_per_sample = 8;
108
 
 
109
 
            // Enable 8khz mode, encode using lower subband only
110
 
            decode_s->eight_k = false;
111
 
 
112
 
            // Never set packed true when using 64 kbps
113
 
            decode_s->packed = false;
114
 
 
115
 
            memset(decode_s->band, 0, sizeof(decode_s->band));
116
 
            decode_s->band[0].det = 32;
117
 
            decode_s->band[1].det = 8;
118
 
 
119
 
            decode_s->in_bits = 0;
120
 
 
121
 
            memset(decode_s->x, 0, sizeof(decode_s->x));
122
 
 
123
 
            decode_s->in_buffer = 0;
124
 
            decode_s->in_bits = 0;
125
 
            decode_s->out_buffer = 0;
126
 
            decode_s->out_bits = 0;
127
 
        }
128
 
 
129
 
        int16_t saturate(int32_t amp) {
130
 
            int16_t amp16 = 0;
 
64
        static void g722_state_init(g722_state_t &state)
 
65
        {
 
66
            state.itu_test_mode = false;
 
67
 
 
68
            // 8 => 64 kbps;  7 => 56 kbps;  6 => 48 kbps
 
69
            state.bits_per_sample = 8;
 
70
 
 
71
            // Enable 8khz mode, encode using lower subband only
 
72
            state.eight_k = false;
 
73
 
 
74
            // Never set packed true when using 64 kbps
 
75
            state.packed = false;
 
76
 
 
77
            memset(state.band, 0, sizeof(state.band));
 
78
            state.band[0].det = 32;
 
79
            state.band[1].det = 8;
 
80
 
 
81
            memset(state.x, 0, sizeof(state.x));
 
82
 
 
83
            state.in_buffer = 0;
 
84
            state.in_bits = 0;
 
85
            state.out_buffer = 0;
 
86
            state.out_bits = 0;
 
87
        }
 
88
 
 
89
        SFLDataFormat saturate(int32_t amp)
 
90
        {
 
91
            SFLDataFormat amp16 = 0;
131
92
 
132
93
            /* Hopefully this is optimised for the common case - not clipping */
133
 
            amp16 = (int16_t) amp;
 
94
            amp16 = (SFLDataFormat) amp;
134
95
 
135
96
            if (amp == amp16)
136
97
                return amp16;
137
98
 
138
99
            if (amp > INT16_MAX)
139
 
                return  INT16_MAX;
 
100
                return INT16_MAX;
140
101
 
141
 
            return  INT16_MIN;
 
102
            return INT16_MIN;
142
103
        }
143
104
 
144
 
 
145
 
        void block4_encode(int band, int d) {
 
105
        void block4_encode(int band, int d)
 
106
        {
146
107
            int wd1 = 0;
147
108
            int wd2 = 0;
148
109
            int wd3 = 0;
149
110
            int i = 0;
150
111
 
151
112
            /* Block 4, RECONS */
152
 
            encode_s->band[band].d[0] = d;
153
 
            encode_s->band[band].r[0] = saturate(encode_s->band[band].s + d);
 
113
            encode_state_.band[band].d[0] = d;
 
114
            encode_state_.band[band].r[0] = saturate(encode_state_.band[band].s + d);
154
115
 
155
116
            /* Block 4, PARREC */
156
 
            encode_s->band[band].p[0] = saturate(encode_s->band[band].sz + d);
 
117
            encode_state_.band[band].p[0] = saturate(encode_state_.band[band].sz + d);
157
118
 
158
119
            /* Block 4, UPPOL2 */
159
120
 
160
121
            for (i = 0;  i < 3;  i++)
161
 
                encode_s->band[band].sg[i] = encode_s->band[band].p[i] >> 15;
162
 
 
163
 
            wd1 = saturate(encode_s->band[band].a[1] << 2);
164
 
 
165
 
            wd2 = (encode_s->band[band].sg[0] == encode_s->band[band].sg[1])  ?  -wd1  :  wd1;
 
122
                encode_state_.band[band].sg[i] = encode_state_.band[band].p[i] >> 15;
 
123
 
 
124
            wd1 = saturate(encode_state_.band[band].a[1] << 2);
 
125
 
 
126
            wd2 = (encode_state_.band[band].sg[0] == encode_state_.band[band].sg[1])  ?  -wd1  :  wd1;
166
127
 
167
128
            if (wd2 > 32767)
168
129
                wd2 = 32767;
169
130
 
170
 
            wd3 = (wd2 >> 7) + ((encode_s->band[band].sg[0] == encode_s->band[band].sg[2])  ?  128  :  -128);
 
131
            wd3 = (wd2 >> 7) + ((encode_state_.band[band].sg[0] == encode_state_.band[band].sg[2])  ?  128  :  -128);
171
132
 
172
 
            wd3 += (encode_s->band[band].a[2]*32512) >> 15;
 
133
            wd3 += (encode_state_.band[band].a[2]*32512) >> 15;
173
134
 
174
135
            if (wd3 > 12288)
175
136
                wd3 = 12288;
176
137
            else if (wd3 < -12288)
177
138
                wd3 = -12288;
178
139
 
179
 
            encode_s->band[band].ap[2] = wd3;
 
140
            encode_state_.band[band].ap[2] = wd3;
180
141
 
181
142
            /* Block 4, UPPOL1 */
182
 
            encode_s->band[band].sg[0] = encode_s->band[band].p[0] >> 15;
183
 
 
184
 
            encode_s->band[band].sg[1] = encode_s->band[band].p[1] >> 15;
185
 
 
186
 
            wd1 = (encode_s->band[band].sg[0] == encode_s->band[band].sg[1])  ?  192  :  -192;
187
 
 
188
 
            wd2 = (encode_s->band[band].a[1]*32640) >> 15;
189
 
 
190
 
            encode_s->band[band].ap[1] = saturate(wd1 + wd2);
191
 
 
192
 
            wd3 = saturate(15360 - encode_s->band[band].ap[2]);
193
 
 
194
 
            if (encode_s->band[band].ap[1] > wd3)
195
 
                encode_s->band[band].ap[1] = wd3;
196
 
            else if (encode_s->band[band].ap[1] < -wd3)
197
 
                encode_s->band[band].ap[1] = -wd3;
 
143
            encode_state_.band[band].sg[0] = encode_state_.band[band].p[0] >> 15;
 
144
 
 
145
            encode_state_.band[band].sg[1] = encode_state_.band[band].p[1] >> 15;
 
146
 
 
147
            wd1 = (encode_state_.band[band].sg[0] == encode_state_.band[band].sg[1])  ?  192  :  -192;
 
148
 
 
149
            wd2 = (encode_state_.band[band].a[1]*32640) >> 15;
 
150
 
 
151
            encode_state_.band[band].ap[1] = saturate(wd1 + wd2);
 
152
 
 
153
            wd3 = saturate(15360 - encode_state_.band[band].ap[2]);
 
154
 
 
155
            if (encode_state_.band[band].ap[1] > wd3)
 
156
                encode_state_.band[band].ap[1] = wd3;
 
157
            else if (encode_state_.band[band].ap[1] < -wd3)
 
158
                encode_state_.band[band].ap[1] = -wd3;
198
159
 
199
160
            /* Block 4, UPZERO */
200
161
            wd1 = (d == 0)  ?  0  :  128;
201
162
 
202
 
            encode_s->band[band].sg[0] = d >> 15;
 
163
            encode_state_.band[band].sg[0] = d >> 15;
203
164
 
204
165
            for (i = 1;  i < 7;  i++) {
205
 
                encode_s->band[band].sg[i] = encode_s->band[band].d[i] >> 15;
206
 
                wd2 = (encode_s->band[band].sg[i] == encode_s->band[band].sg[0])  ?  wd1  :  -wd1;
207
 
                wd3 = (encode_s->band[band].b[i]*32640) >> 15;
208
 
                encode_s->band[band].bp[i] = saturate(wd2 + wd3);
 
166
                encode_state_.band[band].sg[i] = encode_state_.band[band].d[i] >> 15;
 
167
                wd2 = (encode_state_.band[band].sg[i] == encode_state_.band[band].sg[0])  ?  wd1  :  -wd1;
 
168
                wd3 = (encode_state_.band[band].b[i]*32640) >> 15;
 
169
                encode_state_.band[band].bp[i] = saturate(wd2 + wd3);
209
170
            }
210
171
 
211
172
            /* Block 4, DELAYA */
212
173
            for (i = 6;  i > 0;  i--) {
213
 
                encode_s->band[band].d[i] = encode_s->band[band].d[i - 1];
214
 
                encode_s->band[band].b[i] = encode_s->band[band].bp[i];
 
174
                encode_state_.band[band].d[i] = encode_state_.band[band].d[i - 1];
 
175
                encode_state_.band[band].b[i] = encode_state_.band[band].bp[i];
215
176
            }
216
177
 
217
178
            for (i = 2;  i > 0;  i--) {
218
 
                encode_s->band[band].r[i] = encode_s->band[band].r[i - 1];
219
 
                encode_s->band[band].p[i] = encode_s->band[band].p[i - 1];
220
 
                encode_s->band[band].a[i] = encode_s->band[band].ap[i];
 
179
                encode_state_.band[band].r[i] = encode_state_.band[band].r[i - 1];
 
180
                encode_state_.band[band].p[i] = encode_state_.band[band].p[i - 1];
 
181
                encode_state_.band[band].a[i] = encode_state_.band[band].ap[i];
221
182
            }
222
183
 
223
184
            /* Block 4, FILTEP */
224
 
            wd1 = saturate(encode_s->band[band].r[1] + encode_s->band[band].r[1]);
225
 
 
226
 
            wd1 = (encode_s->band[band].a[1]*wd1) >> 15;
227
 
 
228
 
            wd2 = saturate(encode_s->band[band].r[2] + encode_s->band[band].r[2]);
229
 
 
230
 
            wd2 = (encode_s->band[band].a[2]*wd2) >> 15;
231
 
 
232
 
            encode_s->band[band].sp = saturate(wd1 + wd2);
 
185
            wd1 = saturate(encode_state_.band[band].r[1] + encode_state_.band[band].r[1]);
 
186
 
 
187
            wd1 = (encode_state_.band[band].a[1]*wd1) >> 15;
 
188
 
 
189
            wd2 = saturate(encode_state_.band[band].r[2] + encode_state_.band[band].r[2]);
 
190
 
 
191
            wd2 = (encode_state_.band[band].a[2]*wd2) >> 15;
 
192
 
 
193
            encode_state_.band[band].sp = saturate(wd1 + wd2);
233
194
 
234
195
            /* Block 4, FILTEZ */
235
 
            encode_s->band[band].sz = 0;
 
196
            encode_state_.band[band].sz = 0;
236
197
 
237
198
            for (i = 6;  i > 0;  i--) {
238
 
                wd1 = saturate(encode_s->band[band].d[i] + encode_s->band[band].d[i]);
239
 
                encode_s->band[band].sz += (encode_s->band[band].b[i]*wd1) >> 15;
 
199
                wd1 = saturate(encode_state_.band[band].d[i] + encode_state_.band[band].d[i]);
 
200
                encode_state_.band[band].sz += (encode_state_.band[band].b[i]*wd1) >> 15;
240
201
            }
241
202
 
242
 
            encode_s->band[band].sz = saturate(encode_s->band[band].sz);
 
203
            encode_state_.band[band].sz = saturate(encode_state_.band[band].sz);
243
204
 
244
205
            /* Block 4, PREDIC */
245
 
            encode_s->band[band].s = saturate(encode_s->band[band].sp + encode_s->band[band].sz);
 
206
            encode_state_.band[band].s = saturate(encode_state_.band[band].sp + encode_state_.band[band].sz);
246
207
 
247
208
        }
248
209
 
249
 
        void block4_decode(int band, int d) {
 
210
        void block4_decode(int band, int d)
 
211
        {
250
212
            int wd1 = 0;
251
213
            int wd2 = 0;
252
214
            int wd3 = 0;
253
215
            int i = 0;
254
216
 
255
217
            /* Block 4, RECONS */
256
 
            decode_s->band[band].d[0] = d;
257
 
            decode_s->band[band].r[0] = saturate(decode_s->band[band].s + d);
 
218
            decode_state_.band[band].d[0] = d;
 
219
            decode_state_.band[band].r[0] = saturate(decode_state_.band[band].s + d);
258
220
 
259
221
            /* Block 4, PARREC */
260
 
            decode_s->band[band].p[0] = saturate(decode_s->band[band].sz + d);
 
222
            decode_state_.band[band].p[0] = saturate(decode_state_.band[band].sz + d);
261
223
 
262
224
            /* Block 4, UPPOL2 */
263
225
 
264
226
            for (i = 0;  i < 3;  i++)
265
 
                decode_s->band[band].sg[i] = decode_s->band[band].p[i] >> 15;
266
 
 
267
 
            wd1 = saturate(decode_s->band[band].a[1] << 2);
268
 
 
269
 
            wd2 = (decode_s->band[band].sg[0] == decode_s->band[band].sg[1])  ?  -wd1  :  wd1;
 
227
                decode_state_.band[band].sg[i] = decode_state_.band[band].p[i] >> 15;
 
228
 
 
229
            wd1 = saturate(decode_state_.band[band].a[1] << 2);
 
230
 
 
231
            wd2 = (decode_state_.band[band].sg[0] == decode_state_.band[band].sg[1])  ?  -wd1  :  wd1;
270
232
 
271
233
            if (wd2 > 32767)
272
234
                wd2 = 32767;
273
235
 
274
 
            wd3 = (decode_s->band[band].sg[0] == decode_s->band[band].sg[2])  ?  128  :  -128;
 
236
            wd3 = (decode_state_.band[band].sg[0] == decode_state_.band[band].sg[2])  ?  128  :  -128;
275
237
 
276
238
            wd3 += (wd2 >> 7);
277
239
 
278
 
            wd3 += (decode_s->band[band].a[2]*32512) >> 15;
 
240
            wd3 += (decode_state_.band[band].a[2]*32512) >> 15;
279
241
 
280
242
            if (wd3 > 12288)
281
243
                wd3 = 12288;
282
244
            else if (wd3 < -12288)
283
245
                wd3 = -12288;
284
246
 
285
 
            decode_s->band[band].ap[2] = wd3;
 
247
            decode_state_.band[band].ap[2] = wd3;
286
248
 
287
249
            /* Block 4, UPPOL1 */
288
 
            decode_s->band[band].sg[0] = decode_s->band[band].p[0] >> 15;
289
 
 
290
 
            decode_s->band[band].sg[1] = decode_s->band[band].p[1] >> 15;
291
 
 
292
 
            wd1 = (decode_s->band[band].sg[0] == decode_s->band[band].sg[1])  ?  192  :  -192;
293
 
 
294
 
            wd2 = (decode_s->band[band].a[1]*32640) >> 15;
295
 
 
296
 
            decode_s->band[band].ap[1] = saturate(wd1 + wd2);
297
 
 
298
 
            wd3 = saturate(15360 - decode_s->band[band].ap[2]);
299
 
 
300
 
            if (decode_s->band[band].ap[1] > wd3)
301
 
                decode_s->band[band].ap[1] = wd3;
302
 
            else if (decode_s->band[band].ap[1] < -wd3)
303
 
                decode_s->band[band].ap[1] = -wd3;
 
250
            decode_state_.band[band].sg[0] = decode_state_.band[band].p[0] >> 15;
 
251
 
 
252
            decode_state_.band[band].sg[1] = decode_state_.band[band].p[1] >> 15;
 
253
 
 
254
            wd1 = (decode_state_.band[band].sg[0] == decode_state_.band[band].sg[1])  ?  192  :  -192;
 
255
 
 
256
            wd2 = (decode_state_.band[band].a[1]*32640) >> 15;
 
257
 
 
258
            decode_state_.band[band].ap[1] = saturate(wd1 + wd2);
 
259
 
 
260
            wd3 = saturate(15360 - decode_state_.band[band].ap[2]);
 
261
 
 
262
            if (decode_state_.band[band].ap[1] > wd3)
 
263
                decode_state_.band[band].ap[1] = wd3;
 
264
            else if (decode_state_.band[band].ap[1] < -wd3)
 
265
                decode_state_.band[band].ap[1] = -wd3;
304
266
 
305
267
            /* Block 4, UPZERO */
306
268
            wd1 = (d == 0)  ?  0  :  128;
307
269
 
308
 
            decode_s->band[band].sg[0] = d >> 15;
 
270
            decode_state_.band[band].sg[0] = d >> 15;
309
271
 
310
272
            for (i = 1;  i < 7;  i++) {
311
 
                decode_s->band[band].sg[i] = decode_s->band[band].d[i] >> 15;
312
 
                wd2 = (decode_s->band[band].sg[i] == decode_s->band[band].sg[0])  ?  wd1  :  -wd1;
313
 
                wd3 = (decode_s->band[band].b[i]*32640) >> 15;
314
 
                decode_s->band[band].bp[i] = saturate(wd2 + wd3);
 
273
                decode_state_.band[band].sg[i] = decode_state_.band[band].d[i] >> 15;
 
274
                wd2 = (decode_state_.band[band].sg[i] == decode_state_.band[band].sg[0])  ?  wd1  :  -wd1;
 
275
                wd3 = (decode_state_.band[band].b[i]*32640) >> 15;
 
276
                decode_state_.band[band].bp[i] = saturate(wd2 + wd3);
315
277
            }
316
278
 
317
279
            /* Block 4, DELAYA */
318
280
            for (i = 6;  i > 0;  i--) {
319
 
                decode_s->band[band].d[i] = decode_s->band[band].d[i - 1];
320
 
                decode_s->band[band].b[i] = decode_s->band[band].bp[i];
 
281
                decode_state_.band[band].d[i] = decode_state_.band[band].d[i - 1];
 
282
                decode_state_.band[band].b[i] = decode_state_.band[band].bp[i];
321
283
            }
322
284
 
323
285
            for (i = 2;  i > 0;  i--) {
324
 
                decode_s->band[band].r[i] = decode_s->band[band].r[i - 1];
325
 
                decode_s->band[band].p[i] = decode_s->band[band].p[i - 1];
326
 
                decode_s->band[band].a[i] = decode_s->band[band].ap[i];
 
286
                decode_state_.band[band].r[i] = decode_state_.band[band].r[i - 1];
 
287
                decode_state_.band[band].p[i] = decode_state_.band[band].p[i - 1];
 
288
                decode_state_.band[band].a[i] = decode_state_.band[band].ap[i];
327
289
            }
328
290
 
329
291
            /* Block 4, FILTEP */
330
 
            wd1 = saturate(decode_s->band[band].r[1] + decode_s->band[band].r[1]);
331
 
 
332
 
            wd1 = (decode_s->band[band].a[1]*wd1) >> 15;
333
 
 
334
 
            wd2 = saturate(decode_s->band[band].r[2] + decode_s->band[band].r[2]);
335
 
 
336
 
            wd2 = (decode_s->band[band].a[2]*wd2) >> 15;
337
 
 
338
 
            decode_s->band[band].sp = saturate(wd1 + wd2);
 
292
            wd1 = saturate(decode_state_.band[band].r[1] + decode_state_.band[band].r[1]);
 
293
 
 
294
            wd1 = (decode_state_.band[band].a[1]*wd1) >> 15;
 
295
 
 
296
            wd2 = saturate(decode_state_.band[band].r[2] + decode_state_.band[band].r[2]);
 
297
 
 
298
            wd2 = (decode_state_.band[band].a[2]*wd2) >> 15;
 
299
 
 
300
            decode_state_.band[band].sp = saturate(wd1 + wd2);
339
301
 
340
302
            /* Block 4, FILTEZ */
341
 
            decode_s->band[band].sz = 0;
 
303
            decode_state_.band[band].sz = 0;
342
304
 
343
305
            for (i = 6;  i > 0;  i--) {
344
 
                wd1 = saturate(decode_s->band[band].d[i] + decode_s->band[band].d[i]);
345
 
                decode_s->band[band].sz += (decode_s->band[band].b[i]*wd1) >> 15;
 
306
                wd1 = saturate(decode_state_.band[band].d[i] + decode_state_.band[band].d[i]);
 
307
                decode_state_.band[band].sz += (decode_state_.band[band].b[i]*wd1) >> 15;
346
308
            }
347
309
 
348
 
            decode_s->band[band].sz = saturate(decode_s->band[band].sz);
 
310
            decode_state_.band[band].sz = saturate(decode_state_.band[band].sz);
349
311
 
350
312
            /* Block 4, PREDIC */
351
 
            decode_s->band[band].s = saturate(decode_s->band[band].sp + decode_s->band[band].sz);
352
 
        }
353
 
 
354
 
        int g722_encode_release() {
355
 
            delete decode_s;
356
 
            decode_s = NULL;
357
 
            return 0;
358
 
        }
359
 
 
360
 
 
361
 
        int g722_decode_release() {
362
 
            delete encode_s;
363
 
            encode_s = NULL;
364
 
            return 0;
365
 
        }
366
 
 
367
 
        int g722_decode(int16_t amp[], const uint8_t g722_data[], int len) {
 
313
            decode_state_.band[band].s = saturate(decode_state_.band[band].sp + decode_state_.band[band].sz);
 
314
        }
 
315
 
 
316
        int g722_decode(SFLDataFormat amp[], const uint8_t g722_data[], int len)
 
317
        {
368
318
            static const int wl[8] = {-60, -30, 58, 172, 334, 538, 1198, 3042 };
369
319
            static const int rl42[16] = {0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3,  2, 1, 0 };
370
320
            static const int ilb[32] = {
436
386
 
437
387
 
438
388
            for (j = 0;  j < len;) {
439
 
                if (decode_s->packed) {
 
389
                if (decode_state_.packed) {
440
390
                    /* Unpack the code bits */
441
 
                    if (decode_s->in_bits < decode_s->bits_per_sample) {
442
 
                        decode_s->in_buffer |= (g722_data[j++] << decode_s->in_bits);
443
 
                        decode_s->in_bits += 8;
 
391
                    if (decode_state_.in_bits < decode_state_.bits_per_sample) {
 
392
                        decode_state_.in_buffer |= (g722_data[j++] << decode_state_.in_bits);
 
393
                        decode_state_.in_bits += 8;
444
394
                    }
445
395
 
446
 
                    code = decode_s->in_buffer & ((1 << decode_s->bits_per_sample) - 1);
 
396
                    code = decode_state_.in_buffer & ((1 << decode_state_.bits_per_sample) - 1);
447
397
 
448
 
                    decode_s->in_buffer >>= decode_s->bits_per_sample;
449
 
                    decode_s->in_bits -= decode_s->bits_per_sample;
 
398
                    decode_state_.in_buffer >>= decode_state_.bits_per_sample;
 
399
                    decode_state_.in_bits -= decode_state_.bits_per_sample;
450
400
                } else {
451
401
                    code = g722_data[j++];
452
402
                }
453
403
 
454
 
                switch (decode_s->bits_per_sample) {
 
404
                switch (decode_state_.bits_per_sample) {
455
405
 
456
406
                    default:
457
407
 
477
427
                }
478
428
 
479
429
                /* Block 5L, LOW BAND INVQBL */
480
 
                wd2 = (decode_s->band[0].det*wd2) >> 15;
 
430
                wd2 = (decode_state_.band[0].det*wd2) >> 15;
481
431
 
482
432
                /* Block 5L, RECONS */
483
 
                rlow = decode_s->band[0].s + wd2;
 
433
                rlow = decode_state_.band[0].s + wd2;
484
434
 
485
435
                /* Block 6L, LIMIT */
486
436
                if (rlow > 16383)
491
441
                /* Block 2L, INVQAL */
492
442
                wd2 = qm4[wd1];
493
443
 
494
 
                dlowt = (decode_s->band[0].det*wd2) >> 15;
 
444
                dlowt = (decode_state_.band[0].det*wd2) >> 15;
495
445
 
496
446
                /* Block 3L, LOGSCL */
497
447
                wd2 = rl42[wd1];
498
448
 
499
 
                wd1 = (decode_s->band[0].nb*127) >> 7;
 
449
                wd1 = (decode_state_.band[0].nb*127) >> 7;
500
450
 
501
451
                wd1 += wl[wd2];
502
452
 
505
455
                else if (wd1 > 18432)
506
456
                    wd1 = 18432;
507
457
 
508
 
                decode_s->band[0].nb = wd1;
 
458
                decode_state_.band[0].nb = wd1;
509
459
 
510
460
                /* Block 3L, SCALEL */
511
 
                wd1 = (decode_s->band[0].nb >> 6) & 31;
 
461
                wd1 = (decode_state_.band[0].nb >> 6) & 31;
512
462
 
513
 
                wd2 = 8 - (decode_s->band[0].nb >> 11);
 
463
                wd2 = 8 - (decode_state_.band[0].nb >> 11);
514
464
 
515
465
                wd3 = (wd2 < 0)  ? (ilb[wd1] << -wd2)  : (ilb[wd1] >> wd2);
516
466
 
517
 
                decode_s->band[0].det = wd3 << 2;
 
467
                decode_state_.band[0].det = wd3 << 2;
518
468
 
519
469
                block4_decode(0, dlowt);
520
470
 
521
 
                if (!decode_s->eight_k) {
 
471
                if (!decode_state_.eight_k) {
522
472
                    /* Block 2H, INVQAH */
523
473
                    wd2 = qm2[ihigh];
524
 
                    dhigh = (decode_s->band[1].det*wd2) >> 15;
 
474
                    dhigh = (decode_state_.band[1].det*wd2) >> 15;
525
475
                    /* Block 5H, RECONS */
526
 
                    rhigh = dhigh + decode_s->band[1].s;
 
476
                    rhigh = dhigh + decode_state_.band[1].s;
527
477
                    /* Block 6H, LIMIT */
528
478
 
529
479
                    if (rhigh > 16383)
534
484
                    /* Block 2H, INVQAH */
535
485
                    wd2 = rh2[ihigh];
536
486
 
537
 
                    wd1 = (decode_s->band[1].nb*127) >> 7;
 
487
                    wd1 = (decode_state_.band[1].nb*127) >> 7;
538
488
 
539
489
                    wd1 += wh[wd2];
540
490
 
543
493
                    else if (wd1 > 22528)
544
494
                        wd1 = 22528;
545
495
 
546
 
                    decode_s->band[1].nb = wd1;
 
496
                    decode_state_.band[1].nb = wd1;
547
497
 
548
498
                    /* Block 3H, SCALEH */
549
 
                    wd1 = (decode_s->band[1].nb >> 6) & 31;
 
499
                    wd1 = (decode_state_.band[1].nb >> 6) & 31;
550
500
 
551
 
                    wd2 = 10 - (decode_s->band[1].nb >> 11);
 
501
                    wd2 = 10 - (decode_state_.band[1].nb >> 11);
552
502
 
553
503
                    wd3 = (wd2 < 0)  ? (ilb[wd1] << -wd2)  : (ilb[wd1] >> wd2);
554
504
 
555
 
                    decode_s->band[1].det = wd3 << 2;
 
505
                    decode_state_.band[1].det = wd3 << 2;
556
506
 
557
507
                    block4_decode(1, dhigh);
558
508
                }
559
509
 
560
 
                if (decode_s->itu_test_mode) {
561
 
                    amp[outlen++] = (int16_t)(rlow << 1);
562
 
                    amp[outlen++] = (int16_t)(rhigh << 1);
 
510
                if (decode_state_.itu_test_mode) {
 
511
                    amp[outlen++] = (SFLDataFormat)(rlow << 1);
 
512
                    amp[outlen++] = (SFLDataFormat)(rhigh << 1);
563
513
                } else {
564
 
                    if (decode_s->eight_k) {
565
 
                        amp[outlen++] = (int16_t) rlow;
 
514
                    if (decode_state_.eight_k) {
 
515
                        amp[outlen++] = (SFLDataFormat) rlow;
566
516
                    } else {
567
517
                        /* Apply the receive QMF */
568
518
                        for (i = 0;  i < 22;  i++)
569
 
                            decode_s->x[i] = decode_s->x[i + 2];
570
 
 
571
 
                        decode_s->x[22] = rlow + rhigh;
572
 
 
573
 
                        decode_s->x[23] = rlow - rhigh;
 
519
                            decode_state_.x[i] = decode_state_.x[i + 2];
 
520
 
 
521
                        decode_state_.x[22] = rlow + rhigh;
 
522
 
 
523
                        decode_state_.x[23] = rlow - rhigh;
574
524
 
575
525
                        xout1 = 0;
576
526
 
577
527
                        xout2 = 0;
578
528
 
579
529
                        for (i = 0;  i < 12;  i++) {
580
 
                            xout2 += decode_s->x[2*i]*qmf_coeffs[i];
581
 
                            xout1 += decode_s->x[2*i + 1]*qmf_coeffs[11 - i];
 
530
                            xout2 += decode_state_.x[2*i]*qmf_coeffs[i];
 
531
                            xout1 += decode_state_.x[2*i + 1]*qmf_coeffs[11 - i];
582
532
                        }
583
533
 
584
 
                        amp[outlen++] = (int16_t)(xout1 >> 12);
 
534
                        amp[outlen++] = (SFLDataFormat)(xout1 >> 12);
585
535
 
586
 
                        amp[outlen++] = (int16_t)(xout2 >> 12);
 
536
                        amp[outlen++] = (SFLDataFormat)(xout2 >> 12);
587
537
                    }
588
538
                }
589
539
            }
591
541
            return outlen;
592
542
        }
593
543
 
594
 
        int g722_encode(uint8_t g722_data[], const int16_t amp[], int len) {
 
544
        int g722_encode(uint8_t g722_data[], const SFLDataFormat amp[], int len)
 
545
        {
595
546
            static const int q6[32] = {
596
547
                0,   35,   72,  110,  150,  190,  233,  276,
597
548
                323,  370,  422,  473,  530,  587,  650,  714,
669
620
            xhigh = 0;
670
621
 
671
622
            for (j = 0;  j < len;) {
672
 
                if (encode_s->itu_test_mode) {
 
623
                if (encode_state_.itu_test_mode) {
673
624
                    xlow =
674
625
                        xhigh = amp[j++] >> 1;
675
626
                } else {
676
 
                    if (encode_s->eight_k) {
 
627
                    if (encode_state_.eight_k) {
677
628
                        xlow = amp[j++];
678
629
                    } else {
679
630
                        /* Apply the transmit QMF */
680
631
                        /* Shuffle the buffer down */
681
632
                        for (i = 0;  i < 22;  i++)
682
 
                            encode_s->x[i] = encode_s->x[i + 2];
683
 
 
684
 
                        encode_s->x[22] = amp[j++];
685
 
 
686
 
                        encode_s->x[23] = amp[j++];
 
633
                            encode_state_.x[i] = encode_state_.x[i + 2];
 
634
 
 
635
                        encode_state_.x[22] = amp[j++];
 
636
 
 
637
                        encode_state_.x[23] = amp[j++];
687
638
 
688
639
                        /* Discard every other QMF output */
689
640
                        sumeven = 0;
691
642
                        sumodd = 0;
692
643
 
693
644
                        for (i = 0;  i < 12;  i++) {
694
 
                            sumodd += encode_s->x[2*i]*qmf_coeffs[i];
695
 
                            sumeven += encode_s->x[2*i + 1]*qmf_coeffs[11 - i];
 
645
                            sumodd += encode_state_.x[2*i]*qmf_coeffs[i];
 
646
                            sumeven += encode_state_.x[2*i + 1]*qmf_coeffs[11 - i];
696
647
                        }
697
648
 
698
649
                        xlow = (sumeven + sumodd) >> 13;
702
653
                }
703
654
 
704
655
                /* Block 1L, SUBTRA */
705
 
                el = saturate(xlow - encode_s->band[0].s);
 
656
                el = saturate(xlow - encode_state_.band[0].s);
706
657
 
707
658
                /* Block 1L, QUANTL */
708
659
                wd = (el >= 0)  ?  el  :  - (el + 1);
709
660
 
710
661
                for (i = 1;  i < 30;  i++) {
711
 
                    wd1 = (q6[i]*encode_s->band[0].det) >> 12;
 
662
                    wd1 = (q6[i]*encode_state_.band[0].det) >> 12;
712
663
 
713
664
                    if (wd < wd1)
714
665
                        break;
719
670
                /* Block 2L, INVQAL */
720
671
                ril = ilow >> 2;
721
672
                wd2 = qm4[ril];
722
 
                dlow = (encode_s->band[0].det*wd2) >> 15;
 
673
                dlow = (encode_state_.band[0].det*wd2) >> 15;
723
674
 
724
675
                /* Block 3L, LOGSCL */
725
676
                il4 = rl42[ril];
726
 
                wd = (encode_s->band[0].nb*127) >> 7;
727
 
                encode_s->band[0].nb = wd + wl[il4];
 
677
                wd = (encode_state_.band[0].nb*127) >> 7;
 
678
                encode_state_.band[0].nb = wd + wl[il4];
728
679
 
729
 
                if (encode_s->band[0].nb < 0)
730
 
                    encode_s->band[0].nb = 0;
731
 
                else if (encode_s->band[0].nb > 18432)
732
 
                    encode_s->band[0].nb = 18432;
 
680
                if (encode_state_.band[0].nb < 0)
 
681
                    encode_state_.band[0].nb = 0;
 
682
                else if (encode_state_.band[0].nb > 18432)
 
683
                    encode_state_.band[0].nb = 18432;
733
684
 
734
685
                /* Block 3L, SCALEL */
735
 
                wd1 = (encode_s->band[0].nb >> 6) & 31;
 
686
                wd1 = (encode_state_.band[0].nb >> 6) & 31;
736
687
 
737
 
                wd2 = 8 - (encode_s->band[0].nb >> 11);
 
688
                wd2 = 8 - (encode_state_.band[0].nb >> 11);
738
689
 
739
690
                wd3 = (wd2 < 0)  ? (ilb[wd1] << -wd2)  : (ilb[wd1] >> wd2);
740
691
 
741
 
                encode_s->band[0].det = wd3 << 2;
 
692
                encode_state_.band[0].det = wd3 << 2;
742
693
 
743
694
                block4_encode(0, dlow);
744
695
 
745
 
                if (encode_s->eight_k) {
 
696
                if (encode_state_.eight_k) {
746
697
                    /* Just leave the high bits as zero */
747
 
                    code = (0xC0 | ilow) >> (8 - encode_s->bits_per_sample);
 
698
                    code = (0xC0 | ilow) >> (8 - encode_state_.bits_per_sample);
748
699
                } else {
749
700
                    /* Block 1H, SUBTRA */
750
 
                    eh = saturate(xhigh - encode_s->band[1].s);
 
701
                    eh = saturate(xhigh - encode_state_.band[1].s);
751
702
 
752
703
                    /* Block 1H, QUANTH */
753
704
                    wd = (eh >= 0)  ?  eh  :  - (eh + 1);
754
 
                    wd1 = (564*encode_s->band[1].det) >> 12;
 
705
                    wd1 = (564*encode_state_.band[1].det) >> 12;
755
706
                    mih = (wd >= wd1)  ?  2  :  1;
756
707
                    ihigh = (eh < 0)  ?  ihn[mih]  :  ihp[mih];
757
708
 
758
709
                    /* Block 2H, INVQAH */
759
710
                    wd2 = qm2[ihigh];
760
 
                    dhigh = (encode_s->band[1].det*wd2) >> 15;
 
711
                    dhigh = (encode_state_.band[1].det*wd2) >> 15;
761
712
 
762
713
                    /* Block 3H, LOGSCH */
763
714
                    ih2 = rh2[ihigh];
764
 
                    wd = (encode_s->band[1].nb*127) >> 7;
765
 
                    encode_s->band[1].nb = wd + wh[ih2];
 
715
                    wd = (encode_state_.band[1].nb*127) >> 7;
 
716
                    encode_state_.band[1].nb = wd + wh[ih2];
766
717
 
767
 
                    if (encode_s->band[1].nb < 0)
768
 
                        encode_s->band[1].nb = 0;
769
 
                    else if (encode_s->band[1].nb > 22528)
770
 
                        encode_s->band[1].nb = 22528;
 
718
                    if (encode_state_.band[1].nb < 0)
 
719
                        encode_state_.band[1].nb = 0;
 
720
                    else if (encode_state_.band[1].nb > 22528)
 
721
                        encode_state_.band[1].nb = 22528;
771
722
 
772
723
                    /* Block 3H, SCALEH */
773
 
                    wd1 = (encode_s->band[1].nb >> 6) & 31;
 
724
                    wd1 = (encode_state_.band[1].nb >> 6) & 31;
774
725
 
775
 
                    wd2 = 10 - (encode_s->band[1].nb >> 11);
 
726
                    wd2 = 10 - (encode_state_.band[1].nb >> 11);
776
727
 
777
728
                    wd3 = (wd2 < 0)  ? (ilb[wd1] << -wd2)  : (ilb[wd1] >> wd2);
778
729
 
779
 
                    encode_s->band[1].det = wd3 << 2;
 
730
                    encode_state_.band[1].det = wd3 << 2;
780
731
 
781
732
                    block4_encode(1, dhigh);
782
733
 
783
 
                    code = ((ihigh << 6) | ilow) >> (8 - encode_s->bits_per_sample);
 
734
                    code = ((ihigh << 6) | ilow) >> (8 - encode_state_.bits_per_sample);
784
735
                }
785
736
 
786
 
                if (encode_s->packed) {
 
737
                if (encode_state_.packed) {
787
738
                    /* Pack the code bits */
788
 
                    encode_s->out_buffer |= (code << encode_s->out_bits);
789
 
                    encode_s->out_bits += encode_s->bits_per_sample;
 
739
                    encode_state_.out_buffer |= (code << encode_state_.out_bits);
 
740
                    encode_state_.out_bits += encode_state_.bits_per_sample;
790
741
 
791
 
                    if (encode_s->out_bits >= 8) {
792
 
                        g722_data[g722_bytes++] = (uint8_t)(encode_s->out_buffer & 0xFF);
793
 
                        encode_s->out_bits -= 8;
794
 
                        encode_s->out_buffer >>= 8;
 
742
                    if (encode_state_.out_bits >= 8) {
 
743
                        g722_data[g722_bytes++] = (uint8_t)(encode_state_.out_buffer & 0xFF);
 
744
                        encode_state_.out_bits -= 8;
 
745
                        encode_state_.out_buffer >>= 8;
795
746
                    }
796
747
                } else {
797
748
                    g722_data[g722_bytes++] = (uint8_t) code;
801
752
            return g722_bytes;
802
753
        }
803
754
 
804
 
    private:
805
 
        NON_COPYABLE(G722);
806
 
 
807
 
        g722_decode_state_t *decode_s;
808
 
        g722_encode_state_t *encode_s;
809
 
 
 
755
        g722_state_t decode_state_;
 
756
        g722_state_t encode_state_;
810
757
};
811
758
 
812
759
// the class factories
 
760
// cppcheck-suppress unusedFunction
813
761
extern "C" sfl::Codec* create()
814
762
{
815
 
    return new G722(9);
 
763
    return new G722;
816
764
}
817
765
 
 
766
// cppcheck-suppress unusedFunction
818
767
extern "C" void destroy(sfl::Codec* a)
819
768
{
820
769
    delete a;