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