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