1 /*
2  *  Copyright (c) 2012 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_MAIN_SOURCE_STRUCTS_H_
19 #define MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_SOURCE_STRUCTS_H_
20 
21 #include "modules/audio_coding/codecs/isac/bandwidth_info.h"
22 #include "modules/audio_coding/codecs/isac/main/source/settings.h"
23 #include "modules/third_party/fft/fft.h"
24 
25 typedef struct Bitstreamstruct {
26   uint8_t stream[STREAM_SIZE_MAX];
27   uint32_t W_upper;
28   uint32_t streamval;
29   uint32_t stream_index;
30 
31 } Bitstr;
32 
33 typedef struct {
34   double DataBufferLo[WINLEN];
35   double DataBufferHi[WINLEN];
36 
37   double CorrBufLo[ORDERLO + 1];
38   double CorrBufHi[ORDERHI + 1];
39 
40   float PreStateLoF[ORDERLO + 1];
41   float PreStateLoG[ORDERLO + 1];
42   float PreStateHiF[ORDERHI + 1];
43   float PreStateHiG[ORDERHI + 1];
44   float PostStateLoF[ORDERLO + 1];
45   float PostStateLoG[ORDERLO + 1];
46   float PostStateHiF[ORDERHI + 1];
47   float PostStateHiG[ORDERHI + 1];
48 
49   double OldEnergy;
50 
51 } MaskFiltstr;
52 
53 typedef struct {
54   // state vectors for each of the two analysis filters
55   double INSTAT1[2 * (QORDER - 1)];
56   double INSTAT2[2 * (QORDER - 1)];
57   double INSTATLA1[2 * (QORDER - 1)];
58   double INSTATLA2[2 * (QORDER - 1)];
59   double INLABUF1[QLOOKAHEAD];
60   double INLABUF2[QLOOKAHEAD];
61 
62   float INSTAT1_float[2 * (QORDER - 1)];
63   float INSTAT2_float[2 * (QORDER - 1)];
64   float INSTATLA1_float[2 * (QORDER - 1)];
65   float INSTATLA2_float[2 * (QORDER - 1)];
66   float INLABUF1_float[QLOOKAHEAD];
67   float INLABUF2_float[QLOOKAHEAD];
68 
69   /* High pass filter */
70   double HPstates[HPORDER];
71   float HPstates_float[HPORDER];
72 
73 } PreFiltBankstr;
74 
75 typedef struct {
76   // state vectors for each of the two analysis filters
77   double STATE_0_LOWER[2 * POSTQORDER];
78   double STATE_0_UPPER[2 * POSTQORDER];
79 
80   /* High pass filter */
81   double HPstates1[HPORDER];
82   double HPstates2[HPORDER];
83 
84   float STATE_0_LOWER_float[2 * POSTQORDER];
85   float STATE_0_UPPER_float[2 * POSTQORDER];
86 
87   float HPstates1_float[HPORDER];
88   float HPstates2_float[HPORDER];
89 
90 } PostFiltBankstr;
91 
92 typedef struct {
93   // data buffer for pitch filter
94   double ubuf[PITCH_BUFFSIZE];
95 
96   // low pass state vector
97   double ystate[PITCH_DAMPORDER];
98 
99   // old lag and gain
100   double oldlagp[1];
101   double oldgainp[1];
102 
103 } PitchFiltstr;
104 
105 typedef struct {
106   // data buffer
107   double buffer[PITCH_WLPCBUFLEN];
108 
109   // state vectors
110   double istate[PITCH_WLPCORDER];
111   double weostate[PITCH_WLPCORDER];
112   double whostate[PITCH_WLPCORDER];
113 
114   // LPC window   -> should be a global array because constant
115   double window[PITCH_WLPCWINLEN];
116 
117 } WeightFiltstr;
118 
119 typedef struct {
120   // for inital estimator
121   double dec_buffer[PITCH_CORR_LEN2 + PITCH_CORR_STEP2 + PITCH_MAX_LAG / 2 -
122                     PITCH_FRAME_LEN / 2 + 2];
123   double decimator_state[2 * ALLPASSSECTIONS + 1];
124   double hp_state[2];
125 
126   double whitened_buf[QLOOKAHEAD];
127 
128   double inbuf[QLOOKAHEAD];
129 
130   PitchFiltstr PFstr_wght;
131   PitchFiltstr PFstr;
132   WeightFiltstr Wghtstr;
133 
134 } PitchAnalysisStruct;
135 
136 /* Have instance of struct together with other iSAC structs */
137 typedef struct {
138   /* Previous frame length (in ms)                                    */
139   int32_t prev_frame_length;
140 
141   /* Previous RTP timestamp from received
142      packet (in samples relative beginning)                           */
143   int32_t prev_rec_rtp_number;
144 
145   /* Send timestamp for previous packet (in ms using timeGetTime())   */
146   uint32_t prev_rec_send_ts;
147 
148   /* Arrival time for previous packet (in ms using timeGetTime())     */
149   uint32_t prev_rec_arr_ts;
150 
151   /* rate of previous packet, derived from RTP timestamps (in bits/s) */
152   float prev_rec_rtp_rate;
153 
154   /* Time sinse the last update of the BN estimate (in ms)            */
155   uint32_t last_update_ts;
156 
157   /* Time sinse the last reduction (in ms)                            */
158   uint32_t last_reduction_ts;
159 
160   /* How many times the estimate was update in the beginning          */
161   int32_t count_tot_updates_rec;
162 
163   /* The estimated bottle neck rate from there to here (in bits/s)    */
164   int32_t rec_bw;
165   float rec_bw_inv;
166   float rec_bw_avg;
167   float rec_bw_avg_Q;
168 
169   /* The estimated mean absolute jitter value,
170      as seen on this side (in ms)                                     */
171   float rec_jitter;
172   float rec_jitter_short_term;
173   float rec_jitter_short_term_abs;
174   float rec_max_delay;
175   float rec_max_delay_avg_Q;
176 
177   /* (assumed) bitrate for headers (bps)                              */
178   float rec_header_rate;
179 
180   /* The estimated bottle neck rate from here to there (in bits/s)    */
181   float send_bw_avg;
182 
183   /* The estimated mean absolute jitter value, as seen on
184      the other siee (in ms)                                           */
185   float send_max_delay_avg;
186 
187   // number of packets received since last update
188   int num_pkts_rec;
189 
190   int num_consec_rec_pkts_over_30k;
191 
192   // flag for marking that a high speed network has been
193   // detected downstream
194   int hsn_detect_rec;
195 
196   int num_consec_snt_pkts_over_30k;
197 
198   // flag for marking that a high speed network has
199   // been detected upstream
200   int hsn_detect_snd;
201 
202   uint32_t start_wait_period;
203 
204   int in_wait_period;
205 
206   int change_to_WB;
207 
208   uint32_t senderTimestamp;
209   uint32_t receiverTimestamp;
210   // enum IsacSamplingRate incomingStreamSampFreq;
211   uint16_t numConsecLatePkts;
212   float consecLatency;
213   int16_t inWaitLatePkts;
214 
215   IsacBandwidthInfo external_bw_info;
216 } BwEstimatorstr;
217 
218 typedef struct {
219   /* boolean, flags if previous packet exceeded B.N. */
220   int PrevExceed;
221   /* ms */
222   int ExceedAgo;
223   /* packets left to send in current burst */
224   int BurstCounter;
225   /* packets */
226   int InitCounter;
227   /* ms remaining in buffer when next packet will be sent */
228   double StillBuffered;
229 
230 } RateModel;
231 
232 /* The following strutc is used to store data from encoding, to make it
233    fast and easy to construct a new bitstream with a different Bandwidth
234    estimate. All values (except framelength and minBytes) is double size to
235    handle 60 ms of data.
236 */
237 typedef struct {
238   /* Used to keep track of if it is first or second part of 60 msec packet */
239   int startIdx;
240 
241   /* Frame length in samples */
242   int16_t framelength;
243 
244   /* Pitch Gain */
245   int pitchGain_index[2];
246 
247   /* Pitch Lag */
248   double meanGain[2];
249   int pitchIndex[PITCH_SUBFRAMES * 2];
250 
251   /* LPC */
252   int LPCindex_s[108 * 2]; /* KLT_ORDER_SHAPE = 108 */
253   int LPCindex_g[12 * 2];  /* KLT_ORDER_GAIN = 12 */
254   double LPCcoeffs_lo[(ORDERLO + 1) * SUBFRAMES * 2];
255   double LPCcoeffs_hi[(ORDERHI + 1) * SUBFRAMES * 2];
256 
257   /* Encode Spec */
258   int16_t fre[FRAMESAMPLES];
259   int16_t fim[FRAMESAMPLES];
260   int16_t AvgPitchGain[2];
261 
262   /* Used in adaptive mode only */
263   int minBytes;
264 
265 } IsacSaveEncoderData;
266 
267 typedef struct {
268   int indexLPCShape[UB_LPC_ORDER * UB16_LPC_VEC_PER_FRAME];
269   double lpcGain[SUBFRAMES << 1];
270   int lpcGainIndex[SUBFRAMES << 1];
271 
272   Bitstr bitStreamObj;
273 
274   int16_t realFFT[FRAMESAMPLES_HALF];
275   int16_t imagFFT[FRAMESAMPLES_HALF];
276 } ISACUBSaveEncDataStruct;
277 
278 typedef struct {
279   Bitstr bitstr_obj;
280   MaskFiltstr maskfiltstr_obj;
281   PreFiltBankstr prefiltbankstr_obj;
282   PitchFiltstr pitchfiltstr_obj;
283   PitchAnalysisStruct pitchanalysisstr_obj;
284   FFTstr fftstr_obj;
285   IsacSaveEncoderData SaveEnc_obj;
286 
287   int buffer_index;
288   int16_t current_framesamples;
289 
290   float data_buffer_float[FRAMESAMPLES_30ms];
291 
292   int frame_nb;
293   double bottleneck;
294   int16_t new_framelength;
295   double s2nr;
296 
297   /* Maximum allowed number of bits for a 30 msec packet */
298   int16_t payloadLimitBytes30;
299   /* Maximum allowed number of bits for a 30 msec packet */
300   int16_t payloadLimitBytes60;
301   /* Maximum allowed number of bits for both 30 and 60 msec packet */
302   int16_t maxPayloadBytes;
303   /* Maximum allowed rate in bytes per 30 msec packet */
304   int16_t maxRateInBytes;
305 
306   /*---
307     If set to 1 iSAC will not adapt the frame-size, if used in
308     channel-adaptive mode. The initial value will be used for all rates.
309     ---*/
310   int16_t enforceFrameSize;
311 
312   /*-----
313     This records the BWE index the encoder injected into the bit-stream.
314     It will be used in RCU. The same BWE index of main payload will be in
315     the redundant payload. We can not retrieve it from BWE because it is
316     a recursive procedure (WebRtcIsac_GetDownlinkBwJitIndexImpl) and has to be
317     called only once per each encode.
318     -----*/
319   int16_t lastBWIdx;
320 } ISACLBEncStruct;
321 
322 typedef struct {
323   Bitstr bitstr_obj;
324   MaskFiltstr maskfiltstr_obj;
325   PreFiltBankstr prefiltbankstr_obj;
326   FFTstr fftstr_obj;
327   ISACUBSaveEncDataStruct SaveEnc_obj;
328 
329   int buffer_index;
330   float data_buffer_float[MAX_FRAMESAMPLES + LB_TOTAL_DELAY_SAMPLES];
331   double bottleneck;
332   /* Maximum allowed number of bits for a 30 msec packet */
333   // int16_t        payloadLimitBytes30;
334   /* Maximum allowed number of bits for both 30 and 60 msec packet */
335   // int16_t        maxPayloadBytes;
336   int16_t maxPayloadSizeBytes;
337 
338   double lastLPCVec[UB_LPC_ORDER];
339   int16_t numBytesUsed;
340   int16_t lastJitterInfo;
341 } ISACUBEncStruct;
342 
343 typedef struct {
344   Bitstr bitstr_obj;
345   MaskFiltstr maskfiltstr_obj;
346   PostFiltBankstr postfiltbankstr_obj;
347   PitchFiltstr pitchfiltstr_obj;
348   FFTstr fftstr_obj;
349 
350 } ISACLBDecStruct;
351 
352 typedef struct {
353   Bitstr bitstr_obj;
354   MaskFiltstr maskfiltstr_obj;
355   PostFiltBankstr postfiltbankstr_obj;
356   FFTstr fftstr_obj;
357 
358 } ISACUBDecStruct;
359 
360 typedef struct {
361   ISACLBEncStruct ISACencLB_obj;
362   ISACLBDecStruct ISACdecLB_obj;
363 } ISACLBStruct;
364 
365 typedef struct {
366   ISACUBEncStruct ISACencUB_obj;
367   ISACUBDecStruct ISACdecUB_obj;
368 } ISACUBStruct;
369 
370 /*
371   This struct is used to take a snapshot of the entropy coder and LPC gains
372   right before encoding LPC gains. This allows us to go back to that state
373   if we like to limit the payload size.
374 */
375 typedef struct {
376   /* 6 lower-band & 6 upper-band */
377   double loFiltGain[SUBFRAMES];
378   double hiFiltGain[SUBFRAMES];
379   /* Upper boundary of interval W */
380   uint32_t W_upper;
381   uint32_t streamval;
382   /* Index to the current position in bytestream */
383   uint32_t stream_index;
384   uint8_t stream[3];
385 } transcode_obj;
386 
387 typedef struct {
388   // TODO(kwiberg): The size of these tables could be reduced by storing floats
389   // instead of doubles, and by making use of the identity cos(x) =
390   // sin(x+pi/2). They could also be made global constants that we fill in at
391   // compile time.
392   double costab1[FRAMESAMPLES_HALF];
393   double sintab1[FRAMESAMPLES_HALF];
394   double costab2[FRAMESAMPLES_QUARTER];
395   double sintab2[FRAMESAMPLES_QUARTER];
396 } TransformTables;
397 
398 typedef struct {
399   // lower-band codec instance
400   ISACLBStruct instLB;
401   // upper-band codec instance
402   ISACUBStruct instUB;
403 
404   // Bandwidth Estimator and model for the rate.
405   BwEstimatorstr bwestimator_obj;
406   RateModel rate_data_obj;
407   double MaxDelay;
408 
409   /* 0 = adaptive; 1 = instantaneous */
410   int16_t codingMode;
411 
412   // overall bottleneck of the codec
413   int32_t bottleneck;
414 
415   // QMF Filter state
416   int32_t analysisFBState1[FB_STATE_SIZE_WORD32];
417   int32_t analysisFBState2[FB_STATE_SIZE_WORD32];
418   int32_t synthesisFBState1[FB_STATE_SIZE_WORD32];
419   int32_t synthesisFBState2[FB_STATE_SIZE_WORD32];
420 
421   // Error Code
422   int16_t errorCode;
423 
424   // bandwidth of the encoded audio 8, 12 or 16 kHz
425   enum ISACBandwidth bandwidthKHz;
426   // Sampling rate of audio, encoder and decode,  8 or 16 kHz
427   enum IsacSamplingRate encoderSamplingRateKHz;
428   enum IsacSamplingRate decoderSamplingRateKHz;
429   // Flag to keep track of initializations, lower & upper-band
430   // encoder and decoder.
431   int16_t initFlag;
432 
433   // Flag to to indicate signal bandwidth switch
434   int16_t resetFlag_8kHz;
435 
436   // Maximum allowed rate, measured in Bytes per 30 ms.
437   int16_t maxRateBytesPer30Ms;
438   // Maximum allowed payload-size, measured in Bytes.
439   int16_t maxPayloadSizeBytes;
440   /* The expected sampling rate of the input signal. Valid values are 16000
441    * and 32000. This is not the operation sampling rate of the codec. */
442   uint16_t in_sample_rate_hz;
443 
444   // Trig tables for WebRtcIsac_Time2Spec and WebRtcIsac_Spec2time.
445   TransformTables transform_tables;
446 } ISACMainStruct;
447 
448 #endif /* MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_SOURCE_STRUCTS_H_ */
449