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