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 <[email protected]>
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 /*! \file */
25 
26 #if !defined(_G722_H_)
27 #define _G722_H_
28 
29 #include "g722_typedefs.h"
30 
31 /*! \page g722_page G.722 encoding and decoding
32 \section g722_page_sec_1 What does it do?
33 The G.722 module is a bit exact implementation of the ITU G.722 specification for all three
34 specified bit rates - 64000bps, 56000bps and 48000bps. It passes the ITU tests.
35 
36 To allow fast and flexible interworking with narrow band telephony, the encoder and decoder
37 support an option for the linear audio to be an 8k samples/second stream. In this mode the
38 codec is considerably faster, and still fully compatible with wideband terminals using G.722.
39 
40 \section g722_page_sec_2 How does it work?
41 ???.
42 */
43 
44 /* Format DAC12 is added to decode directly into samples suitable for
45    a 12-bit DAC using offset binary representation. */
46 
47 enum {
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) \
57   (uint16_t)((uint16_t)(((s) * (int32_t)(g)) >> 20) + 0x800)
58 #else
59 #define NLDECOMPRESS_APPLY_GAIN(s, g) (((int32_t)(s) * (int32_t)(g)) >> 16)
60 #endif
61 
62 #ifdef BUILD_FEATURE_DAC
63 #define NLDECOMPRESS_PREPROCESS_PCM_SAMPLE_WITH_GAIN(s, g) \
64   NLDECOMPRESS_APPLY_GAIN_CONVERTED_DAC((s), (g))
65 #define NLDECOMPRESS_PREPROCESS_SAMPLE_WITH_GAIN(s, g) ((int16_t)NLDECOMPRESS_APPLY_GAIN((s), (g)))
66 #else
67 #define NLDECOMPRESS_PREPROCESS_PCM_SAMPLE_WITH_GAIN NLDECOMPRESS_PREPROCESS_SAMPLE_WITH_GAIN
68 #define NLDECOMPRESS_PREPROCESS_SAMPLE_WITH_GAIN(s, g) \
69   ((int16_t)(NLDECOMPRESS_APPLY_GAIN((s), (g))))
70 #endif
71 
72 typedef struct {
73   int s;
74   int sp;
75   int sz;
76   int r[3];
77   int a[3];
78   int ap[3];
79   int p[3];
80   int d[7];
81   int b[7];
82   int bp[7];
83   int nb;
84   int det;
85 } g722_band_t;
86 
87 typedef struct {
88   /*! TRUE if the operating in the special ITU test mode, with the band split filters disabled. */
89   int itu_test_mode;
90   /*! TRUE if the G.722 data is packed */
91   int packed;
92   /*! TRUE if encode from 8k samples/second */
93   int eight_k;
94   /*! 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps. */
95   int bits_per_sample;
96 
97   /*! Signal history for the QMF */
98   int x[24];
99 
100   g722_band_t band[2];
101 
102   unsigned int in_buffer;
103   int in_bits;
104   unsigned int out_buffer;
105   int out_bits;
106 } g722_encode_state_t;
107 
108 typedef struct {
109   /*! TRUE if the operating in the special ITU test mode, with the band split filters disabled. */
110   int itu_test_mode;
111   /*! TRUE if the G.722 data is packed */
112   int packed;
113   /*! TRUE if decode to 8k samples/second */
114   int eight_k;
115   /*! 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps. */
116   int bits_per_sample;
117   /*! TRUE if offset binary for a 12-bit DAC */
118   int dac_pcm;
119 
120   /*! Signal history for the QMF */
121   int x[24];
122 
123   g722_band_t band[2];
124 
125   unsigned int in_buffer;
126   int in_bits;
127   unsigned int out_buffer;
128   int out_bits;
129 } g722_decode_state_t;
130 
131 #ifdef __cplusplus
132 extern "C" {
133 #endif
134 
135 g722_encode_state_t *g722_encode_init(g722_encode_state_t *s, unsigned int rate, int options);
136 int g722_encode_release(g722_encode_state_t *s);
137 int g722_encode(g722_encode_state_t *s, uint8_t g722_data[], const int16_t amp[], int len);
138 
139 g722_decode_state_t *g722_decode_init(g722_decode_state_t *s, unsigned int rate, int options);
140 int g722_decode_release(g722_decode_state_t *s);
141 uint32_t g722_decode(g722_decode_state_t *s, int16_t amp[], const uint8_t g722_data[], int len,
142                      uint16_t aGain);
143 
144 #ifdef __cplusplus
145 }
146 #endif
147 
148 #endif
149