1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 /*
12  * structs.h
13  *
14  * This header file contains all the structs used in the ISAC codec
15  *
16  */
17 
18 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_FIX_SOURCE_STRUCTS_H_
19 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_FIX_SOURCE_STRUCTS_H_
20 
21 
22 #include "common_audio/signal_processing/include/signal_processing_library.h"
23 #include "modules/audio_coding/codecs/isac/fix/source/settings.h"
24 #include "typedefs.h"
25 
26 /* Bitstream struct for decoder */
27 typedef struct Bitstreamstruct_dec {
28 
29   WebRtc_UWord16  *stream;          /* Pointer to bytestream to decode */
30   WebRtc_UWord32  W_upper;          /* Upper boundary of interval W */
31   WebRtc_UWord32  streamval;
32   WebRtc_UWord16  stream_index;     /* Index to the current position in bytestream */
33   WebRtc_Word16   full;             /* 0 - first byte in memory filled, second empty*/
34   /* 1 - both bytes are empty (we just filled the previous memory */
35 
36 } Bitstr_dec;
37 
38 /* Bitstream struct for encoder */
39 typedef struct Bitstreamstruct_enc {
40 
41   WebRtc_UWord16  stream[STREAM_MAXW16_60MS];   /* Vector for adding encoded bytestream */
42   WebRtc_UWord32  W_upper;          /* Upper boundary of interval W */
43   WebRtc_UWord32  streamval;
44   WebRtc_UWord16  stream_index;     /* Index to the current position in bytestream */
45   WebRtc_Word16   full;             /* 0 - first byte in memory filled, second empty*/
46   /* 1 - both bytes are empty (we just filled the previous memory */
47 
48 } Bitstr_enc;
49 
50 
51 typedef struct {
52 
53   WebRtc_Word16 DataBufferLoQ0[WINLEN];
54   WebRtc_Word16 DataBufferHiQ0[WINLEN];
55 
56   WebRtc_Word32 CorrBufLoQQ[ORDERLO+1];
57   WebRtc_Word32 CorrBufHiQQ[ORDERHI+1];
58 
59   WebRtc_Word16 CorrBufLoQdom[ORDERLO+1];
60   WebRtc_Word16 CorrBufHiQdom[ORDERHI+1];
61 
62   WebRtc_Word32 PreStateLoGQ15[ORDERLO+1];
63   WebRtc_Word32 PreStateHiGQ15[ORDERHI+1];
64 
65   WebRtc_UWord32 OldEnergy;
66 
67 } MaskFiltstr_enc;
68 
69 
70 
71 typedef struct {
72 
73   WebRtc_Word16 PostStateLoGQ0[ORDERLO+1];
74   WebRtc_Word16 PostStateHiGQ0[ORDERHI+1];
75 
76   WebRtc_UWord32 OldEnergy;
77 
78 } MaskFiltstr_dec;
79 
80 
81 
82 
83 
84 
85 
86 
87 typedef struct {
88 
89   //state vectors for each of the two analysis filters
90 
91   WebRtc_Word32 INSTAT1_fix[2*(QORDER-1)];
92   WebRtc_Word32 INSTAT2_fix[2*(QORDER-1)];
93   WebRtc_Word16 INLABUF1_fix[QLOOKAHEAD];
94   WebRtc_Word16 INLABUF2_fix[QLOOKAHEAD];
95 
96   /* High pass filter */
97   WebRtc_Word32 HPstates_fix[HPORDER];
98 
99 } PreFiltBankstr;
100 
101 
102 typedef struct {
103 
104   //state vectors for each of the two analysis filters
105   WebRtc_Word32 STATE_0_LOWER_fix[2*POSTQORDER];
106   WebRtc_Word32 STATE_0_UPPER_fix[2*POSTQORDER];
107 
108   /* High pass filter */
109 
110   WebRtc_Word32 HPstates1_fix[HPORDER];
111   WebRtc_Word32 HPstates2_fix[HPORDER];
112 
113 } PostFiltBankstr;
114 
115 typedef struct {
116 
117 
118   /* data buffer for pitch filter */
119   WebRtc_Word16 ubufQQ[PITCH_BUFFSIZE];
120 
121   /* low pass state vector */
122   WebRtc_Word16 ystateQQ[PITCH_DAMPORDER];
123 
124   /* old lag and gain */
125   WebRtc_Word16 oldlagQ7;
126   WebRtc_Word16 oldgainQ12;
127 
128 } PitchFiltstr;
129 
130 
131 
132 typedef struct {
133 
134   //for inital estimator
135   WebRtc_Word16   dec_buffer16[PITCH_CORR_LEN2+PITCH_CORR_STEP2+PITCH_MAX_LAG/2-PITCH_FRAME_LEN/2+2];
136   WebRtc_Word32   decimator_state32[2*ALLPASSSECTIONS+1];
137   WebRtc_Word16   inbuf[QLOOKAHEAD];
138 
139   PitchFiltstr  PFstr_wght;
140   PitchFiltstr  PFstr;
141 
142 
143 } PitchAnalysisStruct;
144 
145 
146 typedef struct {
147   /* Parameters used in PLC to avoid re-computation       */
148 
149   /* --- residual signals --- */
150   WebRtc_Word16 prevPitchInvIn[FRAMESAMPLES/2];
151   WebRtc_Word16 prevPitchInvOut[PITCH_MAX_LAG + 10];            // [FRAMESAMPLES/2]; save 90
152   WebRtc_Word32 prevHP[PITCH_MAX_LAG + 10];                     // [FRAMESAMPLES/2]; save 90
153 
154 
155   WebRtc_Word16 decayCoeffPriodic; /* how much to supress a sample */
156   WebRtc_Word16 decayCoeffNoise;
157   WebRtc_Word16 used;       /* if PLC is used */
158 
159 
160   WebRtc_Word16 *lastPitchLP;                                  // [FRAMESAMPLES/2]; saved 240;
161 
162 
163   /* --- LPC side info --- */
164   WebRtc_Word16 lofilt_coefQ15[ ORDERLO ];
165   WebRtc_Word16 hifilt_coefQ15[ ORDERHI ];
166   WebRtc_Word32 gain_lo_hiQ17[2];
167 
168   /* --- LTP side info --- */
169   WebRtc_Word16 AvgPitchGain_Q12;
170   WebRtc_Word16 lastPitchGain_Q12;
171   WebRtc_Word16 lastPitchLag_Q7;
172 
173   /* --- Add-overlap in recovery packet --- */
174   WebRtc_Word16 overlapLP[ RECOVERY_OVERLAP ];                 // [FRAMESAMPLES/2]; saved 160
175 
176   WebRtc_Word16 pitchCycles;
177   WebRtc_Word16 A;
178   WebRtc_Word16 B;
179   WebRtc_Word16 pitchIndex;
180   WebRtc_Word16 stretchLag;
181   WebRtc_Word16 *prevPitchLP;                                  // [ FRAMESAMPLES/2 ]; saved 240
182   WebRtc_Word16 seed;
183 
184   WebRtc_Word16 std;
185 } PLCstr;
186 
187 
188 
189 /* Have instance of struct together with other iSAC structs */
190 typedef struct {
191 
192   WebRtc_Word16   prevFrameSizeMs;      /* Previous frame size (in ms) */
193   WebRtc_UWord16  prevRtpNumber;      /* Previous RTP timestamp from received packet */
194   /* (in samples relative beginning)  */
195   WebRtc_UWord32  prevSendTime;   /* Send time for previous packet, from RTP header */
196   WebRtc_UWord32  prevArrivalTime;      /* Arrival time for previous packet (in ms using timeGetTime()) */
197   WebRtc_UWord16  prevRtpRate;          /* rate of previous packet, derived from RTP timestamps (in bits/s) */
198   WebRtc_UWord32  lastUpdate;           /* Time since the last update of the Bottle Neck estimate (in samples) */
199   WebRtc_UWord32  lastReduction;        /* Time sinse the last reduction (in samples) */
200   WebRtc_Word32   countUpdates;         /* How many times the estimate was update in the beginning */
201 
202   /* The estimated bottle neck rate from there to here (in bits/s)                */
203   WebRtc_UWord32  recBw;
204   WebRtc_UWord32  recBwInv;
205   WebRtc_UWord32  recBwAvg;
206   WebRtc_UWord32  recBwAvgQ;
207 
208   WebRtc_UWord32  minBwInv;
209   WebRtc_UWord32  maxBwInv;
210 
211   /* The estimated mean absolute jitter value, as seen on this side (in ms)       */
212   WebRtc_Word32   recJitter;
213   WebRtc_Word32   recJitterShortTerm;
214   WebRtc_Word32   recJitterShortTermAbs;
215   WebRtc_Word32   recMaxDelay;
216   WebRtc_Word32   recMaxDelayAvgQ;
217 
218 
219   WebRtc_Word16   recHeaderRate;         /* (assumed) bitrate for headers (bps) */
220 
221   WebRtc_UWord32  sendBwAvg;           /* The estimated bottle neck rate from here to there (in bits/s) */
222   WebRtc_Word32   sendMaxDelayAvg;    /* The estimated mean absolute jitter value, as seen on the other siee (in ms)  */
223 
224 
225   WebRtc_Word16   countRecPkts;          /* number of packets received since last update */
226   WebRtc_Word16   highSpeedRec;        /* flag for marking that a high speed network has been detected downstream */
227 
228   /* number of consecutive pkts sent during which the bwe estimate has
229      remained at a value greater than the downstream threshold for determining highspeed network */
230   WebRtc_Word16   countHighSpeedRec;
231 
232   /* flag indicating bwe should not adjust down immediately for very late pckts */
233   WebRtc_Word16   inWaitPeriod;
234 
235   /* variable holding the time of the start of a window of time when
236      bwe should not adjust down immediately for very late pckts */
237   WebRtc_UWord32  startWaitPeriod;
238 
239   /* number of consecutive pkts sent during which the bwe estimate has
240      remained at a value greater than the upstream threshold for determining highspeed network */
241   WebRtc_Word16   countHighSpeedSent;
242 
243   /* flag indicated the desired number of packets over threshold rate have been sent and
244      bwe will assume the connection is over broadband network */
245   WebRtc_Word16   highSpeedSend;
246 
247 
248 
249 
250 } BwEstimatorstr;
251 
252 
253 typedef struct {
254 
255   /* boolean, flags if previous packet exceeded B.N. */
256   WebRtc_Word16    PrevExceed;
257   /* ms */
258   WebRtc_Word16    ExceedAgo;
259   /* packets left to send in current burst */
260   WebRtc_Word16    BurstCounter;
261   /* packets */
262   WebRtc_Word16    InitCounter;
263   /* ms remaining in buffer when next packet will be sent */
264   WebRtc_Word16    StillBuffered;
265 
266 } RateModel;
267 
268 /* The following strutc is used to store data from encoding, to make it
269    fast and easy to construct a new bitstream with a different Bandwidth
270    estimate. All values (except framelength and minBytes) is double size to
271    handle 60 ms of data.
272 */
273 typedef struct {
274 
275   /* Used to keep track of if it is first or second part of 60 msec packet */
276   int     startIdx;
277 
278   /* Frame length in samples */
279   WebRtc_Word16         framelength;
280 
281   /* Pitch Gain */
282   WebRtc_Word16   pitchGain_index[2];
283 
284   /* Pitch Lag */
285   WebRtc_Word32   meanGain[2];
286   WebRtc_Word16   pitchIndex[PITCH_SUBFRAMES*2];
287 
288   /* LPC */
289   WebRtc_Word32         LPCcoeffs_g[12*2]; /* KLT_ORDER_GAIN = 12 */
290   WebRtc_Word16   LPCindex_s[108*2]; /* KLT_ORDER_SHAPE = 108 */
291   WebRtc_Word16   LPCindex_g[12*2];  /* KLT_ORDER_GAIN = 12 */
292 
293   /* Encode Spec */
294   WebRtc_Word16   fre[FRAMESAMPLES];
295   WebRtc_Word16   fim[FRAMESAMPLES];
296   WebRtc_Word16   AvgPitchGain[2];
297 
298   /* Used in adaptive mode only */
299   int     minBytes;
300 
301 } ISAC_SaveEncData_t;
302 
303 typedef struct {
304 
305   Bitstr_enc          bitstr_obj;
306   MaskFiltstr_enc     maskfiltstr_obj;
307   PreFiltBankstr      prefiltbankstr_obj;
308   PitchFiltstr        pitchfiltstr_obj;
309   PitchAnalysisStruct pitchanalysisstr_obj;
310   RateModel           rate_data_obj;
311 
312   WebRtc_Word16         buffer_index;
313   WebRtc_Word16         current_framesamples;
314 
315   WebRtc_Word16      data_buffer_fix[FRAMESAMPLES]; // the size was MAX_FRAMESAMPLES
316 
317   WebRtc_Word16         frame_nb;
318   WebRtc_Word16         BottleNeck;
319   WebRtc_Word16         MaxDelay;
320   WebRtc_Word16         new_framelength;
321   WebRtc_Word16         s2nr;
322   WebRtc_UWord16        MaxBits;
323 
324   WebRtc_Word16         bitstr_seed;
325 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED
326   PostFiltBankstr     interpolatorstr_obj;
327 #endif
328 
329   ISAC_SaveEncData_t *SaveEnc_ptr;
330   WebRtc_Word16         payloadLimitBytes30; /* Maximum allowed number of bits for a 30 msec packet */
331   WebRtc_Word16         payloadLimitBytes60; /* Maximum allowed number of bits for a 30 msec packet */
332   WebRtc_Word16         maxPayloadBytes;     /* Maximum allowed number of bits for both 30 and 60 msec packet */
333   WebRtc_Word16         maxRateInBytes;      /* Maximum allowed rate in bytes per 30 msec packet */
334   WebRtc_Word16         enforceFrameSize;    /* If set iSAC will never change packet size */
335 
336 } ISACFIX_EncInst_t;
337 
338 
339 typedef struct {
340 
341   Bitstr_dec          bitstr_obj;
342   MaskFiltstr_dec     maskfiltstr_obj;
343   PostFiltBankstr     postfiltbankstr_obj;
344   PitchFiltstr        pitchfiltstr_obj;
345   PLCstr              plcstr_obj;               /* TS; for packet loss concealment */
346 
347 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED
348   PreFiltBankstr      decimatorstr_obj;
349 #endif
350 
351 } ISACFIX_DecInst_t;
352 
353 
354 
355 typedef struct {
356 
357   ISACFIX_EncInst_t ISACenc_obj;
358   ISACFIX_DecInst_t ISACdec_obj;
359   BwEstimatorstr     bwestimator_obj;
360   WebRtc_Word16         CodingMode;       /* 0 = adaptive; 1 = instantaneous */
361   WebRtc_Word16   errorcode;
362   WebRtc_Word16   initflag;  /* 0 = nothing initiated; 1 = encoder or decoder */
363   /* not initiated; 2 = all initiated */
364 } ISACFIX_SubStruct;
365 
366 
367 typedef struct {
368   WebRtc_Word32   lpcGains[12];     /* 6 lower-band & 6 upper-band we may need to double it for 60*/
369   /* */
370   WebRtc_UWord32  W_upper;          /* Upper boundary of interval W */
371   WebRtc_UWord32  streamval;
372   WebRtc_UWord16  stream_index;     /* Index to the current position in bytestream */
373   WebRtc_Word16   full;             /* 0 - first byte in memory filled, second empty*/
374   /* 1 - both bytes are empty (we just filled the previous memory */
375   WebRtc_UWord16  beforeLastWord;
376   WebRtc_UWord16  lastWord;
377 } transcode_obj;
378 
379 
380 //Bitstr_enc myBitStr;
381 
382 #endif  /* WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_FIX_SOURCE_STRUCTS_H_ */
383