1 /*
2  * SpanDSP - a series of DSP components for telephony
3  *
4  * g722.h - The ITU G.722 codec.
5  *
6  * Written by Steve Underwood <steveu@coppice.org>
7  *
8  * Copyright (C) 2005 Steve Underwood
9  *
10  *  Despite my general liking of the GPL, I place my own contributions
11  *  to this code in the public domain for the benefit of all mankind -
12  *  even the slimy ones who might try to proprietize my work and use it
13  *  to my detriment.
14  *
15  * Based on a single channel G.722 codec which is:
16  *
17  *****    Copyright (c) CMU    1993      *****
18  * Computer Science, Speech Group
19  * Chengxiang Lu and Alex Hauptmann
20  *
21  * $Id: g722.h 48959 2006-12-25 06:42:15Z rizzo $
22  */
23 
24 
25 /*! \file */
26 
27 #if !defined(_G722_H_)
28 #define _G722_H_
29 
30 /*! \page g722_page G.722 encoding and decoding
31 \section g722_page_sec_1 What does it do?
32 The G.722 module is a bit exact implementation of the ITU G.722 specification for all three
33 specified bit rates - 64000bps, 56000bps and 48000bps. It passes the ITU tests.
34 
35 To allow fast and flexible interworking with narrow band telephony, the encoder and decoder
36 support an option for the linear audio to be an 8k samples/second stream. In this mode the
37 codec is considerably faster, and still fully compatible with wideband terminals using G.722.
38 
39 \section g722_page_sec_2 How does it work?
40 ???.
41 */
42 
43 /* Format DAC12 is added to decode directly into samples suitable for
44    a 12-bit DAC using offset binary representation. */
45 
46 enum
47 {
48     G722_SAMPLE_RATE_8000 = 0x0001,
49     G722_PACKED = 0x0002,
50     G722_FORMAT_DAC12 = 0x0004,
51 };
52 
53 #ifdef BUILD_FEATURE_DAC
54 #define NLDECOMPRESS_APPLY_GAIN(s,g) (((s) * (int32_t)(g)) >> 16)
55 // Equivalent to shift 16, add 0x8000, shift 4
56 #define NLDECOMPRESS_APPLY_GAIN_CONVERTED_DAC(s,g) (uint16_t)((uint16_t)(((s) * (int32_t)(g)) >> 20) + 0x800)
57 #else
58 #define NLDECOMPRESS_APPLY_GAIN(s,g) (((int32_t)(s) * (int32_t)(g)) >> 16)
59 #endif
60 
61 #ifdef BUILD_FEATURE_DAC
62 #define NLDECOMPRESS_PREPROCESS_PCM_SAMPLE_WITH_GAIN(s,g) NLDECOMPRESS_APPLY_GAIN_CONVERTED_DAC((s),(g))
63 #define NLDECOMPRESS_PREPROCESS_SAMPLE_WITH_GAIN(s,g) ((int16_t)NLDECOMPRESS_APPLY_GAIN((s),(g)))
64 #else
65 #define NLDECOMPRESS_PREPROCESS_PCM_SAMPLE_WITH_GAIN NLDECOMPRESS_PREPROCESS_SAMPLE_WITH_GAIN
66 #define NLDECOMPRESS_PREPROCESS_SAMPLE_WITH_GAIN(s,g) ((int16_t)(NLDECOMPRESS_APPLY_GAIN((s),(g))))
67 #endif
68 
69 typedef struct {
70     int s;
71     int sp;
72     int sz;
73     int r[3];
74     int a[3];
75     int ap[3];
76     int p[3];
77     int d[7];
78     int b[7];
79     int bp[7];
80     int nb;
81     int det;
82 } g722_band_t;
83 
84 typedef struct
85 {
86     /*! TRUE if the operating in the special ITU test mode, with the band split filters
87              disabled. */
88     int itu_test_mode;
89     /*! TRUE if the G.722 data is packed */
90     int packed;
91     /*! TRUE if encode from 8k samples/second */
92     int eight_k;
93     /*! 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps. */
94     int bits_per_sample;
95 
96     /*! Signal history for the QMF */
97     int x[24];
98 
99     g722_band_t band[2];
100 
101     unsigned int in_buffer;
102     int in_bits;
103     unsigned int out_buffer;
104     int out_bits;
105 } g722_encode_state_t;
106 
107 typedef struct
108 {
109     /*! TRUE if the operating in the special ITU test mode, with the band split filters
110              disabled. */
111     int itu_test_mode;
112     /*! TRUE if the G.722 data is packed */
113     int packed;
114     /*! TRUE if decode to 8k samples/second */
115     int eight_k;
116     /*! 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps. */
117     int bits_per_sample;
118     /*! TRUE if offset binary for a 12-bit DAC */
119     int dac_pcm;
120 
121     /*! Signal history for the QMF */
122     int x[24];
123 
124     g722_band_t band[2];
125 
126     unsigned int in_buffer;
127     int in_bits;
128     unsigned int out_buffer;
129     int out_bits;
130 } g722_decode_state_t;
131 
132 #ifdef __cplusplus
133 extern "C" {
134 #endif
135 
136 g722_encode_state_t *g722_encode_init(g722_encode_state_t *s, unsigned int rate, int options);
137 int g722_encode_release(g722_encode_state_t *s);
138 int g722_encode(g722_encode_state_t *s, uint8_t g722_data[], const int16_t amp[], int len);
139 
140 g722_decode_state_t *g722_decode_init(g722_decode_state_t *s, unsigned int rate, int options);
141 int g722_decode_release(g722_decode_state_t *s);
142 uint32_t g722_decode(g722_decode_state_t *s, int16_t amp[], const uint8_t g722_data[], int len, uint16_t aGain);
143 
144 #ifdef __cplusplus
145 }
146 #endif
147 
148 #endif
149