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